Where is the content of <?php echo $this->getChildHtml(’right’) ?>

Urvisha picture Urvisha · Apr 16, 2013 · Viewed 40.6k times · Source

I’m trying to re-organise my right sidebar. In the template (2columns-right), it calls :

<?php echo $this->getChildHtml('right') ?>

Where can I find the content of this variable?

Answer

Jonathan Hussey picture Jonathan Hussey · Apr 16, 2013

A call to method getChildHtml() loads the HTML for the child block with the name which is passed to the method, so in this case we are looking for a child block named right.

To determine where to find this child block we need to know which block is calling this method. I know that that particular call to the getChildHtml() method appears in the main page column template as right is one of the columns. So have a look in the page.xml layout file and search for the template file inside which you found the method call, you will find something like this:

<reference name="root">
    <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
    <!-- Mark root page block that template is applied -->
    <action method="setIsHandle"><applied>1</applied></action>
</reference>

Using the <reference> tag in a layout file allows you to alter the targeted block, and the <action> tag allows you to run a block method inside the block you are working with. So this section of layout sets the template inside the root block. From this we know that it is the root block calling the getChildHtml() method.

Next lets look at where the root block is defined in the layout, it's in the same page.xml layout file, and should be right near the top:

<block type="page/html" name="root" output="toHtml" template="page/3columns.phtml">
    ...
    <block type="core/text_list" name="right" as="right" translate="label">
        <label>Right Column</label>
    </block>
    ...
</block>

There is quite a lot defined in this block, but you can see that it is given the name root and defines quite a few child blocks. One of these child blocks is named right and it is this block whose HTML is being output by the getChildHtml() method. It is important to note the block type - core/text_list. This is a special block type which means that when rendering out the HTML for this block using the getChildHtml() method, child blocks will also be rendered. If the block type was page/html like with the root block, every child block added to the right block would need it's own getChildHtml() method call, using this block type, you only need a call to getChildHtml('right') and all child blocks will also be rendered.

As you can see the right block is defined here but it is empty. This is because in exactly the same way as your have the tag referencing the root block (<reference name="root">) other layout files add child blocks to the right block by referencing the right block.

<reference name="right">
    ...
</reference>

So to finally answer your question (and hopefully inform a little along the way), you need to be looking in layout files other than page.xml for references to the right block, here you will find all of the child content output by the getChildHtml() method call.

You can alter what is added to the right block in your own module layout file, or local.xml layout file if you are not creating a module. I briefly cover the local.xml layout file in my answer here with example syntax to add new blocks and remove blocks added in other layout files.