Difference Between and in Magento Layout

blockslayout

My current understanding of defining layout blocks is that if I do:

<block type="core/template" name="a.b" as="aB" template="some.phtml">
    <block type="x/y" name="c.d" as="cD" />
</block>

then I've added c.d as a child block of a.b, and from within some.phtml I can call $this->getChildHtml('cD'); to ask c.d to render its output at that position in the template.

I was looking around in catalog.xml, and I've just come across the syntax:

<action method="append"><block>block.name</block></action>

so I followed it to see what's different from the previous method:

public function append($block, $alias = '')
{
    $this->insert($block, '', true, $alias);
    return $this;
}

....

public function insert($block, $siblingName = '', $after = false, $alias = '')

append implies ordering the child block at the end of the parent block, but I could have easily done that with an after="-" parameter in the first method.

So, what's the difference between these two methods of adding a child block to a parent block?

Best Answer

append appends an existing block that is referenced by name, to a new parent. It can be used to move a block to another location or to duplicate it.

<block> always creates a new block.