Magento中,调用静态块的几种方法

在后台创建一个order_form静态块
Block Title :Order Form
Identifier :order_form
Status :Enabled
Content :自定义内容

1.如果要在.phtml文件中直接调用这个静态块,那可以采用以下方法

  1. <?php  
  2. $block = Mage::getModel('cms/block')  
  3.  ->setStoreId(Mage::app()->getStore()->getId())  
  4.  ->load('order_form');  
  5. $content = $block->getContent(); // Block的原始内容已经获得  
  6.   
  7. $processor = Mage::getModel('core/email_template_filter');  
  8. echo $html = $processor->filter($content);  
  9. ?>  


Mage::getModel('core/email_template_filter')->filter()是必须的,因为Static Block里可能包含Magento的模板语言(如:{{store url=""}}),fiter将翻译成实际的值
Magento中调用静态Block主要有两个地方。
是否感觉这代码太长了呢,那你还可以这么写

  1. <?php echo $this->getLayout()->createBlock(‘cms/block’)->setBlockId('order_form')->toHtml() ?>   

2.如何在CMS页面的Content中调用这个静态块呢?你可以采用以下方法

  1. {{block type="cms/block"  name="cms_test_block"  block_id="order_form" }}   


将里面order_form改成你的静态块对应的block_id则可

3.怎么样在layout中调用静态块呢?

[xhtml] view plaincopy
  1.  <reference name="footer">  
  2.     <block type="cms/block" name="order_form" before="-">  
  3.         <action method="setBlockId"><block_id>order_form</block_id></action>  
  4.     </block>      
  5. </reference>   

 
 
到此,你应该能够灵活的运用magento中的静态块了吧!

原文地址:https://www.cnblogs.com/focai/p/4166778.html