Magento Layout: getChildHtml() with Empty Result

The problem: Mysteriously, getChildHtml() yields an empty result in the template, although the child blocks were created.

The solution: Blocks in the Magento layout should always have a name. Without name-attribute they are shown, but child elements cannot be assigned and stay “orphaned”.

The responsible method is Mage_Core_Model_Layout::_generateBlock(). As you can see, the parent block only gets assigned if it has a name:

            $parentName = $parent->getBlockName();
            if (!empty($parentName)) {
                $parentBlock = $this->getBlock($parentName);
            }

Note that $parent is an XML node here, not a block object. So it does not help that blocs without name automatically get a name like ANONYMOUS_1. The assignment happens via  name-attribute of the node, so that it works for <reference> as well as for <block> parents.

Leave a Reply

Your email address will not be published. Required fields are marked *