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()
?
With method output="toHtml" in layout the block is redered on end of document. I tested with
And magento rendered my block after tag html end
I found an elegant solution using Observer and wrote a post on the company blog: http://www.fxplabs.com.br/blog/instanciando-blocos-em-qualquer-lugar-magento/
translation to english:
http://translate.google.com/translate?sl=pt&tl=en&js=n&prev=_t&hl=pt-BR&ie=UTF-8&eotf=1&u=http%3A%2F%2Fwww.fxplabs.com.br%2Fblog%2Finstanciando-blocos-em-qualquer-lugar-magento%2F
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
Then in your template.phtml file you would have:
As I said, this won't fit every situation and it's not incredibly elegant, but it does work.
Jon
It cap possible by add to xml - following method output="toHtml" - will put block to parent block
I believe you cannot output a block without having it in a template that is shown. So if you make your extension on top of creating your block you need to call it in the template where you want it. Unfortunately that means that someone could add your extension on their website and if they have created their own copy of the template file where your block is added because they wanted to modify it in their theme, well your block will not show there. They will have to figure out themselves that your module needs a modification of a template and put the modification in their own template themselves.
A block can output itself without being called in a template if you add the
output="toHtml"
attribute to it but as some others have pointed out this will cause the block to be rendered after the</html>
tag. That is normal because theoutput="toHtml"
is what makes the root block work. The root block cannot be included in a template because it has no parent so it uses theoutput="toHtml"
attribute and it makes it come out. That works because when Magento renders its layout (builds the HTML of the page) it calls the getOutput() method of the Mage_Core_Model_Layout class and this method really just gets all blocks which have an output param and pukes them out on the page 1 by 1. Normally you should have only 2 such blocks, root and "core_profiler". (yes that means that the profiler will show after the closing HTML tag).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.