Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 895
Ricardo Martins Avatar asked Aug 10 '12 19:08

Ricardo Martins


1 Answers

I solved the problem by using setChild method as follows:

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

And so I can use

<?php echo $this->getChildHtml('somealias'); ?>
like image 131
Ricardo Martins Avatar answered Oct 14 '22 02:10

Ricardo Martins