Magento. Insert block into another without change template code

前端 未结 7 1901
耶瑟儿~
耶瑟儿~ 2021-02-06 01:36

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 file

7条回答
  •  [愿得一人]
    2021-02-06 01:56

    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 tag. That is normal because the output="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 the output="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).

提交回复
热议问题