Magento. Insert block into another without change template code
Solution 1
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.
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.
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>
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.
Jon
Solution 2
No, there is not a generic way to add your block to any other block. The reason it works occasionally for you is that there are some block types that just enumerate their children (core/text_list
being one of those) and some templates manually do the same (using $this->getChild()
).
If you want to add your block underneath a block that fits neither of these criteria, you will need to modify the template to echo that block.
Solution 3
You can try add to xml - following method output="toHtml" - will put block to parent block But...
<reference name="product.info">
<block type='googlethis/link' name="googlethis" output="to Html" template="catalog/product/googlethis.phtml"/>
</reference>
Solution 4
It cap possible by add to xml - following method output="toHtml" - will put block to parent block
<reference name="product.info">
<block type='googlethis/link' name="googlethis" as="googlethis" output="toHtml" template="catalog/product/googlethis.phtml"/>
</reference>
Solution 5
With method output="toHtml" in layout the block is redered on end of document. I tested with
<catalog_product_view>
<reference name="media">
<block type="pricetag/catalog_product_view" name="catalog.product.price.tag" template="pricetag/price.phtml" output="toHtml" />
</reference>
</catalog_product_view>
And magento rendered my block after tag html end
Yaroslav Rogoza
Web software engineer focused on eCommerce. Zend Certified Engineer (PHP 5.3) Magento Certified Developer Plus Magento Certified Frontend Developer CTO at Atwix company
Updated on June 09, 2022Comments
-
Yaroslav Rogoza almost 2 years
I've tried to find solution but with no results. My task is to write module. It should insert some html into existing block.
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>
and my block shows as well.
In other cases I should call
getChildHtml()
method and it's not good because it makes to change template .phtml files.So is there way to insert my phtml block into any other phtml block without calling
getChildHtml()
? -
Yaroslav Rogoza almost 13 yearsAlso. Can we detect somehow which block can be used as reference in layout configuration for output without getChildHtml() and which can't? Thanks
-
Joe Mastey almost 13 yearsChecking for the block type of
core/text_list
would be the first step. Otherwise, grep the template directory for the$this->getChild()
calls. -
Jonathan Day almost 13 yearsthat's a neat solution, I like it
-
Yaroslav Rogoza almost 12 yearsGreat! That's what I've been looking for!
-
Stanley about 11 yearsplease write about your answer here instead of posting the link, thanks!