Magento XML using before/after to place blocks hardly ever works

前端 未结 2 444

I\'m a front-end Magento dev, have built quite a few of my own themes and I want to understand Magento\'s XML block positioning better...

I normally use a loca

2条回答
  •  失恋的感觉
    2020-12-04 13:06

    The before and after attributes only work in one of two cases:

    1. When you insert into a core/text_list block
    2. When your template block calls getChildHtml without any parameters

    When you say

    
       
    
    

    you're telling Magento

    Hey Magento, put the example_block inside the root block.

    When you put a number of different blocks inside a parent, those blocks have an implicit order. For template blocks, this order doesn't matter, since those blocks are being explicitly rendered.

    getChildHtml('example_block') ?>
    

    However, there's two cases where order matters. First, if you call

    getChildHtml() ?>
    

    from a template, then Magento will render all the child blocks, in order.

    Secondly, there's a special type of block called a "text list" (core/text_list/Mage_Core_Block_Text_List). These blocks render all their children automatically, again in order. The content block is an example of this

    
    

    That's why you can insert blocks into content and they render automatically.

    So, in your example above, you're inserting blocks into the root block. The root block is a template block whose phtml template uses getChildHtml calls with explicit parameters. Therefore the before and after attributes don't do what you (and many others, including me) wish they did.

提交回复
热议问题