Search code examples
magentomagento-1.9

How to create a child block programmatically?


I have a page that has some template block included programmatically as follows:

public function indexAction() {
    $this->loadLayout();
    $block = $this->getLayout()
        ->createBlock('core/template')
        ->setTemplate('somefolder/sometemplate.phtml');
    
    $this->getLayout()->getBlock('content')->append($block);
    $this->renderLayout();

}

I would like to put inside the sometemplate.phtml, $this->getChildHtml('somechild') to insert another block.

I tried

$box = $this->getLayout()
->createBlock('page/html')
->setTemplate('somefolder/somechild.phtml');
$block->append($box);

But it didn't work. How can I do it?


Solution

  • I solved the problem by using setChild method as follows:

    $block->setChild('somealias',$childBlock);
    

    And so I can use

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