64

From the following line in 2columns-right.phtml

<div class="col-main">
    <?php echo $this->getChildHtml('global_messages') ?>
    <?php echo $this->getChildHtml('content') ?>
</div>

I am not able to understand where the content in <?php echo $this->getChildHtml('content') ?> is coming from.

Which .phtml file is called to display the data by <?php echo $this->getChildHtml('content') ?>?

mickmackusa
  • 43,625
  • 12
  • 83
  • 136
Anup_Tripathi
  • 2,817
  • 3
  • 26
  • 37

1 Answers1

156

If we're discussing the frontend of the website, the particular line you've asked about....

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

is added to the Magento layout XML in app/design/frontend/base/default/layout/page.xml. In Magento version 1.8, you'll find it defined in lines 92-94.

<block type="core/text_list" name="content" as="content" translate="label">
    <label>Main Content Area</label>
</block>

By looking at the "type" attribute of this block tag, we can know what object class this section of the layout is. It comes from the "Core" module, and is of the block type Text List. The class name for this Mage_Core_Block_Text_List. (app/code/core/Mage/Core/Block/Text/List.php). Text Lists are simply block containers which purpose is to store additional child blocks inside them. You can add any number of child blocks to the text list and they will be rendered out either in the order they were added or the order they've been assigned.

So, to answer your question, there is no view script (.phtml file) that renders the contents of $this->getChildHtml('content'). The blocks which have been added to this block, may themselves have view scripts associated with them. To find out what view scripts those are, you'd have to find the layout XML which has added the block.

For example, if I had the following layout file added to the frontend of my website's theme:

<?xml version="1.0"?>
<layout>
    <default>
        <reference name="content">
            <block type="core/template" name="my_view_script" template="hello/world.phtml" />
        </reference>
    </default>
</layout>

The code above, would add the block with an object class of Mage_Core_Block_Template to the block with the name 'content' (which happens to be the one you asked about). Magento will then look for the view script in the following locations, in this order:

app/design/frontend/PACKAGE_NAME/THEME_NAME/template/hello/world.phtml
app/design/frontend/PACKAGE_NAME/default/template/hello/world.phtml
app/design/frontend/base/default/template/hello/world.phtml

First one that is found, is the one it will use. If no view script is found Magento will log an error in var/logs/system.log (default log file setting) stating that the view script was not found. No output from the block will occur.

Note that depending on your settings in System -> Configuration -> (General) Design, there may be additional package/theme locations Magento will look in. There are also other scenarios such as if the "Custom Theme" is field is changed for individual CMS Pages, Catalog Categories, or Catalog Products, these individual model's view page may have an additional view script location (that will match the selected theme) that takes precedence over your site's default settings.

Magento will follow this same fallback logic when looking for translation files as well as layout XML files.

Please note, that it is perfectly acceptable to copy individual view scripts (avoid copying entire directories, copy over only view scripts you actually intend to modify) from app/design/frontend/base/default/template/ to your local theme, and customize them for the purposes of your website's theme. However, in order to have an upgrade compatible site, layout files should not be copied from base to your local theme. Doing so, does not follow upgrade compatible practices. Instead, XML Layout updates for your theme should be contained in app/design/frontend/PACKAGE_NAME/THEME_NAME/layout/local.xml. There is no layout instructions from app/design/frontend/base/default/layout/*, that cannot be removed/added-to/changed, what-have-you, with the proper XML instructions in local.xml.

Darren Felton
  • 2,299
  • 1
  • 18
  • 18
  • 1
    What does it refer if no argument passed? ie getChildHtml() ?> – Anto S May 27 '15 at 11:12
  • 1
    @version_0.1 When no string argument for a block name is passed to `getChildHtml() ?>` then all child blocks of `$this` will be rendered in order. Even if they've already been output higher up in the same view script. – Darren Felton May 27 '15 at 15:24
  • 1
    You have much key information in this comprehensive answer but I very much needed the "...XML layout updates for you theme should be contained in..." portion. Awesome! Thanks. – Roralee Aug 04 '15 at 18:18
  • 1
    Most useful description I've found on the subject, and oh how I have searched. You should write a blog post on this, the internet is lacking resources on this topic ;) – Chris J Allen Apr 02 '17 at 20:13
  • Thank you @ChrisJAllen :) A blog would be an interesting goal to aim for. I have enjoyed my time in Magento thus far. – Darren Felton Apr 04 '17 at 13:45