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

Sandy44818

New Member
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 local.xml file to manipulate everything, I can define a block as follows:\[code\]<cms_index_index> <reference name="root"> <block type="core/template" name="example_block" as="exampleBlock" template="page/html/example-block.phtml"/> </reference></cms_index_index>\[/code\]This would create a block on the home page (cms_index_index) and since the block is created one level under 'root', I would normally call the block by adding:\[code\]<?php echo $this->getChildHtml('exampleBlock') ?>\[/code\]...to 1column.phtml (or 2columns-left/right.phtml, 3columns.phtml etc). The block can be placed on any page by substituting 'cms_index_index' for the appropriate page tag.I see stuff like the following throughout the core XML files, and in tutorials:\[code\]<reference name="root"> <block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/></reference>\[/code\]'content' is a block which is part of magento's general page structure and, from what I understand, before="content" should place it where you'd expect, without needing to use getChildHtml('exampleBlock'), so far so good... however, before/after hardly ever seems to work for me, and I frequently find myself resorting to the getChildHtml method as backup, which isn't always ideal, and means editing more .phtml files than necessary. I've tried:\[code\]<reference name="root"> <block type="core/template" name="example_block" before="content" template="page/html/example-block.phtml"/></reference>\[/code\]Nothing appears...\[code\]<reference name="root"> <block type="core/template" name="example_block" after="header" template="page/html/example-block.phtml"/></reference>\[/code\]Still nothing.... I'm also aware of using before="-" or after="-" to place something before everything within it's parent block. I occasionally have some luck with that, but generally just get confused and frustrated.I've googled all over the place for 'magento xml before/after not working' and beginning to wonder if its just me this happens to... can anyone explain when I can and can't use before/after to position blocks? What's wrong with the above examples?I'm in magento 1.7.0.2 (latest available at time of posting)The main motivation for this is to reduce the number of core .phtml files I need to edit just to add a getChildHtml(), so if there is another (XML) way to get around this I'd be interested to know...
 
Back
Top