Magento. Insert block into another without change template code

前端 未结 7 1913
耶瑟儿~
耶瑟儿~ 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 </html> 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).

    0 讨论(0)
  • 2021-02-06 01:57

    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.

    0 讨论(0)
  • 2021-02-06 02:00

    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

    0 讨论(0)
  • 2021-02-06 02:05

    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>
    
    0 讨论(0)
  • 2021-02-06 02:07

    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>
    
    0 讨论(0)
  • 2021-02-06 02:11

    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

    0 讨论(0)
提交回复
热议问题