静态块(static block),仅包含一些静态的html内容,不涉及数据库存取,比如像:一些文字和图片链接,网站页脚部分等。建立static block很简单,Magento后台提供一个功能,可以方便的创建、编辑、管理static block。可以在【管理员后台】》【CMS】》【Static Blocks】菜单找到。 <default> <reference name="footer"> <block type="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 --> <action method="setBlockId"><block_id>footer_links</block_id></action> </block> </reference> </default> 然后,在模板文件footer.phtml中输出: 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> */ class Mage_Cms_Block_Block extends Mage_Core_Block_Abstract { protected function _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; } } (责任编辑:最模板) |