This is the source for my doubt. Please have a a look on this question, answer and answer's comment.
Basically the question demands to include a core/template
block inside header
block. For this we can use this code in layout XML.
<reference name="header">
<block type="core/template" name="test_name" as="test_as" template="test/test.phtml" />
</reference>
and the test.phtml
look like this.
<p><?php echo $this->__('Can you see me ?'); ?></p>
In order to see the content of my template, I need to call my test
block inside header
block as like this. This is because, header
block is of type page/html_header
and the block will not automatically convert its content to html as other core block does. So this is how we can call test
block in header
block.
<div><?php echo $this->getChildHtml('test_as') ?></div>
Note that, I am using the as
property of test
block to invoke that block in header
block. Upto this everything is clear for me. But the actual doubt starts here.
Doubt :- If I didn't specify the as
property in my test
block and if I call my test
block and then invokes the block as like this,
<div><?php echo $this->getChildHtml('test_name'); ?></div>
it works and show the content of test.phtml
in frontend. This also works.
<div><?php echo $this->getChildHtml(); ?></div>
How it is possible to show the block output, by invoking that block with its name in its parent block ????
I am asking this because, there were lot of occasions in which I didn't specify as property that leaves a block not rendered. (even if I use name property of my block)
I know, magic happens here. But I can't just understand what exactly happens here.
public function getChildHtml($name = '', $useCache = true, $sorted = false)
{
if ($name === '') {
if ($sorted) {
$children = array();
foreach ($this->getSortedChildren() as $childName) {
$children[$childName] = $this->getLayout()->getBlock($childName);
}
} else {
$children = $this->getChild();
}
$out = '';
foreach ($children as $child) {
$out .= $this->_getChildHtml($child->getBlockAlias(), $useCache);
}
return $out;
} else {
return $this->_getChildHtml($name, $useCache);
}
}
So any help mostly appreciated .
Thanks in Advance