Children of Anonymous Blocks are Orphaned

Posted . Visible to the public.

What a sad state of affairs. If a childs parent has no name, the child block becomes orphaned and alone, somewhere lost in Layout XML hell. To sort it, give the parent a name. The reason? The layout XML parser uses the name of the parent in XML to set the parent of the child block class instance, so if there is no name, the child is orphaned.

This won't work:

<block type="core/text_list">
    <block type="core/template" template="a/valid/template.phtml" />
</block>

This will:

<block type="core/text_list" name="whatever_the_hell_you_want">
    <block type="core/template" template="a/valid/template.phtml" />
</block>

Magento gives parent blocks an anonymous name, so this doesn't have to be the case. Children need not be orphaned, but that's just how it goes.

Mike Whitby
Posted by Mike Whitby to Magento (2014-05-16 15:11)