Magento调用静态块 static block
生活随笔
收集整理的這篇文章主要介紹了
Magento调用静态块 static block
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
靜態塊(static block),僅包含一些靜態的html內容,不涉及數據庫存取,比如像:一些文字和圖片鏈接,網站頁腳部分等。建立static block很簡單,Magento后臺提供一個功能,可以方便的創建、編輯、管理static block。可以在【管理員后臺】》【CMS】》【Static Blocks】菜單找到。
建立了static block后,如何在前端界面顯示呢?一是在Magento的layout文件中配置,然后在模板文件.phtml中通過調用 getChildHtml('block_id')輸出為html代碼。下面借助在Magento中系統內置的一個靜態塊footer_links來說明。
首先,在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; } }
轉載于:https://my.oschina.net/u/234530/blog/56430
總結
以上是生活随笔為你收集整理的Magento调用静态块 static block的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将访问的文件夹变为磁盘盘符-摘自网络
- 下一篇: java reflect 例子