繁体   English   中英

如何在phtml模板中调用Magento块?

[英]How to call Magento block in phtml template?

我需要在页脚中显示更多链接。 我在magento admin中创建了这些链接作为静态块(id = sample_links)。

然后我添加了以下代码page.xml文件

<reference name="foot_lnk">  
<block type="cms/block" name="sample_block" before="-">
      <action method="setBlockId"><block_id>sample_links</block_id></action>
    </block>
</reference>

我在footer.phtml中称这个为,

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

但它不显示CMS静态块内容。 问题是什么?

$this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml() 

引用是先前定义的块,您希望块在内部,例如:

<reference name="footer">
  <block type="cms/block" name="sample_links">
    <action method="setBlockId"><block_id>sample_links</block_id></action>
  </block>
</reference>

然后

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

你可以调用一个statick块,如:

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

并调用一个块,如:

<?php echo $this->getLayout()->createBlock('sidebar/left')->setTemplate('bannerslider/left.phtml')->tohtml(); ?>

访问magevn.com查看更多用例在magento中使用块。

如果你不想打扰XML,就像swapnesh的答案一样,我只是让它更清晰的那里的php noobs(像我一样)

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

your_identifier是您在CMS> Blocks> Create New Block中创建块时决定使用的代码,第二行称为“Identifier”

将您的引用名称更改为页脚

喜欢

<reference name="footer">  

然后它会工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM