Magento:在 phtml 文件中将静态块作为 html 获取

2022-08-30 12:06:36

我有一个名为(带有内容)的静态块,我想在文件上将其显示为.newest_product.phtmlhtml

我试过这个代码:

echo $this->getLayout()->createBlock('cms/block')->setBlockId('newest_product')->toHtml(); 

但这一点没有显示出来。

我是否使用了错误的代码?


答案 1

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

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

答案 2

在布局中(应用/设计/前端/your_theme/布局/默认.xml):

<default>
    <cms_page> <!-- need to be redefined for your needs -->
        <reference name="content">
            <block type="cms/block" name="cms_newest_product" as="cms_newest_product">
                <action method="setBlockId"><block_id>newest_product</block_id></action>
            </block>
        </reference>
    </cms_page>
</default>

在 phtml 模板中:

<?php echo $this->getChildHtml('newest_product'); ?>

不要忘记缓存清理。

我认为这很有帮助。


推荐