我有一个名为newest_product
(带内容)的静态块,我想将其显示在.phtml
文件中html.
我试过这段代码:
echo $this->getLayout()->createBlock('cms/block')->setBlockId('newest_product')->toHtml();
Run Code Online (Sandbox Code Playgroud)
但这一切都没有显示出来.
我使用错误的代码吗?
Sum*_*P4U 77
如果您已从管理面板创建名为"block_identifier"的CMS块.接下来将是用.phtml调用它们的代码
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml();
?>
Run Code Online (Sandbox Code Playgroud)
Max*_*nko 50
在布局中(app/design/frontend/your_theme/layout/default.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>
Run Code Online (Sandbox Code Playgroud)
在你的phtml模板中:
<?php echo $this->getChildHtml('newest_product'); ?>
Run Code Online (Sandbox Code Playgroud)
不要忘记缓存清理.
我认为这有帮助.
小智 21
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() ?>
Run Code Online (Sandbox Code Playgroud)
并使用此链接获取更多信息 http://www.justwebdevelopment.com/blog/how-to-call-static-block-in-magento/
Jer*_*oen 12
如果要将cmsblock加载到模板/块文件/模型等中,可以按照以下步骤操作.这将使任何变量位于cmsblock中
$block = Mage::getModel('cms/block')
->setStoreId(Mage::app()->getStore()->getId())
->load('identifier');
$var = array('variable' => 'value', 'other_variable' => 'other value');
/* This will be {{var variable}} and {{var other_variable}} in your CMS block */
$filterModel = Mage::getModel('cms/template_filter');
$filterModel->setVariables($var);
echo $filterModel->filter($block->getContent());
Run Code Online (Sandbox Code Playgroud)
我认为这对你有用
$block = Mage::getModel('cms/block')->setStoreId(Mage::app()->getStore()->getId())->load('newest_product');
echo $block->getTitle();
echo $block->getContent();
Run Code Online (Sandbox Code Playgroud)
它确实有效,但现在CMS块中的变量不再解析了:(
小智 5
当您在Magento中调用CMS-Static Block时,以下代码将起作用.
<?php echo
$this->getLayout()->createBlock('cms/block')->setBlockId('block_identifier')->toHtml();
?>
Run Code Online (Sandbox Code Playgroud)