php 如何在 phtml 模板中调用 Magento 块?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10647650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call Magento block in phtml template?
提问by Ravichandran Jothi
i need to display some more links in footer. i created those links in magento admin as static blocks (id = sample_links ).
我需要在页脚中显示更多链接。我在 magento admin 中创建了这些链接作为静态块(id = sample_links)。
and then i added following code page.xml file
然后我添加了以下代码 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>
i called this one in footer.phtml as,
我在footer.phtml中称这个为,
<?php echo $this->getChildHtml('foot_lnk') ?>
but it does not display the CMS static block content. what is the issue?.
但它不显示 CMS 静态块内容。这是什么问题?。
回答by swapnesh
$this->getLayout()->createBlock('cms/block')->setBlockId('my_static_block_name')->toHtml()
回答by Alex Hadley
The reference is the block previously defined that you want your block to be inside, e.g.:
引用是先前定义的块,您希望块位于其中,例如:
<reference name="footer">
<block type="cms/block" name="sample_links">
<action method="setBlockId"><block_id>sample_links</block_id></action>
</block>
</reference>
Then
然后
<?php echo $this->getChildHtml('sample_links') ?>
回答by Phù du ??i
You can call a statick block like:
您可以调用 statick 块,例如:
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your_identifier')->toHtml() ?>
And call a block like:
并调用一个块,如:
<?php echo $this->getLayout()->createBlock('sidebar/left')->setTemplate('bannerslider/left.phtml')->tohtml(); ?>
Visit magevn.comto see more usecase to use block in magento.
访问magevn.com查看更多用例以在 magento 中使用块。
回答by Hiboomedia
If you don't want to bother with XML, same as swapnesh's answer, I'm just making it clearer for the php noobs out there (like me)
如果你不想打扰 XML,就像 swapnesh 的回答一样,我只是让 php noobs 更清楚(像我一样)
<?php echo $this->getLayout()->createBlock('cms/block')->setBlockId('your_identifier')->toHtml() ?>
your_identifier is the code you decide to use when creating your block in CMS > Blocks > Create New Block, second line called "Identifier"
your_identifier 是您在 CMS > Blocks > Create New Block 中创建块时决定使用的代码,第二行称为“标识符”
回答by Mano Karthick
change your reference name to footer
将您的参考名称更改为页脚
like
喜欢
<reference name="footer">
then it will work.
那么它会起作用。

