Magento Module Development Series – Part3 – Advanced Layouts

In this blog we will look in some more advanced function that can be used on layout files.

In the previous blog we looked at some basics of layouts files.

Reference Blocks: Add Child, Function calls, Remove Block

Reference blocks were already explained in the previous blog post. Here are some more operation that we can do on a block by taking reference of a block.

Add Child

This is how it is done.

<reference name='left'>
   <block type='core/template' name='child' template='child.phtml'/>

In this “left” is name of another block, in our case it’s defined in page.xml. So basically here we are adding a child block to another block using reference. The main purpose of using child blocks, is to make code neat and clean i.e we are able to divided layout into many xml files and still have it inter related.

Function Call

Now if you want to call a function on another block using reference, this is how we do it, In customer.xml you will find this.

<reference name="top.links">
<action method="addLink"><label>My Account</label><url helper="customer/getAccountUrl"/><title>My Account</title><prepare/><urlParams/><position>10</position></action>

So the, <action> tag is used to call a function on a block which has name as “top.links”. “top.links “ block is define in page.xml. There is the block declaration

<block type="page/template_links" name="top.links" as="topLinks"/>

As we can see the block class of top.links block is Mage_Page_Block_Template_Links. What the <action> tag does it that, it calls the addLink function in the class Mage_Page_Block_Template_Links and pass it parameters.
If you open the Links.php file, this is the definition of the addLinks function

public function addLink($label, $url='', $title='', $prepare=false, $urlParams=array(), $position=null, $liParams=null, $aParams=null, $beforeText='', $afterText='');

so you can clearly see how parameters are passed using xml.

Remove Block

You can remove a child block from a parent block using reference. This is done in many places in magento e.g

<reference name="left">
    <remove name="block1"/>

What this does is obvious,  it simply removes the block with name “block1” from the parent block left.

Update Tag

You will find update tag used in various place in magento especially in customer.xml e.g

<update handle="customer_account"/>

What this does is simple, it simply copies all blocks and other tag from the parent tag <customer_account> to the block inside which we are calling the <update> tag.

Block Ordering

As we know, we can add as many child block as we want to a parent block. But in case of blocks like left,right and content where the child blocks gets displayed automatically and we don’t need to call the $this->getChildHtml() function. But now the question comes, how do we set which blocks get displayed first and which later. So, to solve this we have before and after attributes that we can set in the block we created.

<block type='core/template' template='page/test.phtml' before='block_name' name='test'/>

So our test block will show before ‘block_name’ similarly we can use after=’block_name’ as well.
Now, before=”-” means, it will display before all other blocks, so this would be first block. And after=’-‘ means, this would be the last block.


  • Create a new file test.phtml in catalog folder. this block should show up in all magento pages in the right column, except the category page.
  • Add a link to the top links in magento i.e in My Account, My Cart, etc. Add a new link called “Orders” which points to Order History Page of Customer. Hints