Magento:在一个phtml文件中获得一个静态块作为html

我有一个名为newest_product (含内容)的静态块,我想将它显示在一个.phtml文件为HTML

我试过这个代码:

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

但这没有什么显示。

我使用错误的代码?

在布局(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> 

在您的phtml模板中:

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

不要忘记caching清理。

我认为这有帮助。

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

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

并使用此链接更多http://www.justwebdevelopment.com/blog/how-to-call-static-block-in-magento/

如果你想加载一个cmsblock到你的模板/块文件/模型等。你可以这样做如下。 这将呈现cmsblock中的任何variables位置

 $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()); 

我认为这会为你工作

 $block = Mage::getModel('cms/block')->setStoreId(Mage::app()->getStore()->getId())->load('newest_product'); echo $block->getTitle(); echo $block->getContent(); 

它确实有效,但现在CMS块中的variables不再parsing:(

当您在Magento中调用CMS-Static Block时,以下代码将起作用。

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

这应该作为testing工作。

 <?php $filter = new Mage_Widget_Model_Template_Filter(); $_widget = $filter->filter('{{widget type="cms/widget_page_link" template="cms/widget/link/link_block.phtml" page_id="2"}}'); echo $_widget; ?> 

当您从pipe理面板创build一个名为block_identifier的新CMS模块时,您可以使用以下代码从.phtml文件调用它:

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

然后清除caching并重新加载浏览器。