Magento.将块插入另一个而不更改模板代码 [英] Magento. Insert block into another without change template code
问题描述
我试图找到解决方案,但没有结果. 我的任务是编写模块.它应该在现有代码块中插入一些html.
I've tried to find solution but with no results. My task is to write module. It should insert some html into existing block.
我注意到,当我使用布局.xml文件时,我可以将块插入诸如
I noticed that when I used layout .xml files I can just insert my block into some reference like
<reference name="product.info">
<block type='googlethis/link' name="googlethis"
template="catalog/product/googlethis.phtml"/>
</reference>
我的方块也显示出来.
在其他情况下,我应该调用getChildHtml()
方法,但这并不好,因为它会更改模板.phtml文件.
In other cases I should call getChildHtml()
method and it's not good because it makes to change template .phtml files.
那么有没有办法在不调用getChildHtml()
的情况下将我的phtml块插入任何个其他phtml块中?
So is there way to insert my phtml block into any other phtml block without calling getChildHtml()
?
推荐答案
虽然不是一个完美的解决方案,但是有一种方法可以做到这一点.不过,它在大多数情况下都可以运行,并且有时会被证明是有用的.
There is a way to do this, although it is not an entirely elegant solution. It will work in most instances though and has proved helpful on occasion.
基本上,您的想法是替换要在布局XML中之前/之后呈现块的块,将其作为子代放置在块中,然后在其之前/之后呈现输出.
Basically the idea is that you replace the block you want to render your block before/after in your layout XML, place that block as a child in your block and then render it's output before/after yours.
因此,假设您想在购物车详细信息页面上的totals块之前输出一个块,则可以在扩展程序的layout.xml中执行以下操作
So let's say you wanted to output a block before the totals block on the cart details page, you could do the following in your extension's layout.xml
<checkout_cart_index>
<reference name="checkout.cart">
<block type="myextension/block" name="myextension.block" as="myextension_block" template="myextension/template.phtml">
<action method="setChild"><name>totals</name><block>totals</block></action>
</block>
<action method="setChild"><name>totals</name><block>myextension.block</block></action>
</reference>
</checkout_cart_index>
然后在您的template.phtml文件中将具有以下内容:
Then in your template.phtml file you would have:
<div id="myextension">
// Your template code
</div>
// Render the totals block that you placed inside your block
<?php echo $this->getChildHtml('totals'); ?>
就像我说的那样,这并不适合所有情况,虽然它不是很优雅,但是确实可以.
As I said, this won't fit every situation and it's not incredibly elegant, but it does work.
乔恩
这篇关于Magento.将块插入另一个而不更改模板代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!