在由phtml文件插入的view.phtml中调用静态块


14

我们正在视图页面中显示文本字段,以检查COD的可用性。

在此处输入图片说明

view.phtml

echo $this->getLayout()->createBlock('core/template')->setTemplate('checkdelivery/checkdelivery.phtml')->toHtml();

view.phtml我们下面的文件中调用。但是我想调用静态块标识符 [identifier code-check]而不是下面的文件。

模板/checkdelivery/checkdelivery.phtml

<div class="block block-list block-check-delivery">
    <div class="block-title">
        <?php $blockLabel = Mage::getStoreConfig('checkdelivery/general/block_title'); ?>
        <strong><span><?php echo $this->__($blockLabel) ?></span></strong>
    </div>
    <div class="block-content" >        
        <br>
            <input name="zipcode" size="17" type="text" id="zipcode" value="<?php echo Mage::getModel('core/cookie')->get('zip'); ?>" maxlength="10" class="input-text" placeholder="<?php echo $this->__('Enter ZIP Code'); ?>"/>
            <button type="button" name="zip-check" title="Check" class="button" id="zip-check" ><span><?php echo $this->__('Check'); ?></span></button>
            <div id="delivery-message"></div>
            <?php $defaultHtml = Mage::getStoreConfig('checkdelivery/general/default_html'); ?>
            <div id="delivery-html"><?php if(Mage::getModel('core/cookie')->get('message')){
    echo Mage::getModel('core/cookie')->get('message');
}
else{
    $defaultHtml; } ?></div>

        <br>        
    </div>

</div>

<script>
    Event.observe('zip-check', 'click', function(event){
        new Ajax.Request("<?php echo $this->getUrl('checkdelivery/index/index') ?>", {
            method: "get",
            parameters: {zipcode : $('zipcode').value },
            onSuccess: function(transport) {
                 var json = transport.responseText.evalJSON();
                 $('delivery-message').update(json.message);                 
                 $('delivery-message').setStyle({ color: json.color});
                 $('delivery-html').update(json.html);  
            }
        });
    });
</script>

静态块代码:

<p>{{block type ="core/template" template = "checkdelivery/checkdelivery.phtml"}}</p>

Answers:


44

您可以使用以下代码:

在.phtml文件中:

<?php
  echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); 
?> 

样本:

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('home')->toHtml(); ?> 

在静态块/ cms页面中:

{{block type="core/template" template="checkdelivery/checkdelivery.phtml"}}


现在不显示文本字段。我更新了有问题的静态块代码。
Magento中的婴儿,

您是否刷新了缓存?
Qaisar Satti

是的,删除了缓存文件夹,还删除了浏览器缓存。
Magento中的婴儿,

1
{{block type ="core/template" template="checkdelivery/checkdelivery.phtml" }}删除空间并检查system->permission ->block是否已列出(核心/模板)?
Qaisar Satti

1
用createBlock('Magento \ Cms \ Block \ Block')代替-> createBlock('cms / block')对我说::谢谢您的提示!
Rustyjim

3

如果您已从管理面板创建了名为“ block_identifier”的CMS块。然后下面是在.phtml中调用它们的代码

<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml(); 
?> 

清除缓存并重新加载浏览器。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.