静态块(static block),仅包含一些静态的html内容,不涉及数据库存取,好比像:一些文字和图片连接,网站页脚部分等。创建static block很简单,Magento后台提供一个功能,能够方便的建立、编辑、管理static block。能够在【管理员后台】》【CMS】》【Static Blocks】菜单找到。 php
创建了static block后,如何在前端界面显示呢?一是在Magento的layout文件中配置,而后在模板文件.phtml中经过调用 getChildHtml('block_id')输出为html代码。下面借助在Magento中系统内置的一个静态块footer_links来讲明。 html
首先,在cms.xml layout文件中设置静态块:前端
<default> <referencename="footer"> <blocktype="cms/block"name="cms_footer_links"before="footer_links"> <!-- The content of this block is taken from the database by its block_id. You can manage it in admin CMS -> Static Blocks --> <actionmethod="setBlockId"><block_id>footer_links</block_id></action> </block> </reference> </default>
而后,在模板文件footer.phtml中输出:
// echo $this->getChildHtml(); echo $this->getChildHtml('footer_links');
另一种方式更简单,不须要配置layout文件,就能够直接在php代码中输出静态块内容: 数据库
echo $this->getLayout()->createBlock('cms/block')->setBlockId('footer_links')->toHtml();
确实很简单,但Magento在背后作了大量的工做,在文件app/code/core/Mage/Cms/Block/Block.php中,能够看到这些辛苦的步伐:
/** * Cms block content * * @category Mage * @package Mage_Cms * @author Magento Core Team <core@magentocommerce.com> */ classMage_Cms_Block_Block extendsMage_Core_Block_Abstract { protectedfunction_toHtml() { if(!$this->_beforeToHtml()) { return''; } $html= ''; if($blockId= $this->getBlockId()) { $block= Mage::getModel('cms/block') ->setStoreId(Mage::app()->getStore()->getId()) ->load($blockId); if(!$block->getIsActive()) { $html= ''; } else{ $content= $block->getContent(); $processor= Mage::getModel('core/email_template_filter'); $html= $processor->filter($content); } } return$html; } }