Magento – Adding a Child Block Programmatically

blocks

I'm creating a block, and I wish it to have several child blocks that appear prior to the rendering of my main block.

Following the standards set in other, inherited classes, I can see the following:

public function _prepareLayout()
{
    $this->setChild('alias',
        $this->getLayout()->createBlock('modulename/block_name')
    );
    return parent::_prepareLayout();
}

If I follow the path to the parent _prepareLayout() function, it appears in exactly the same format. In addition, if I print out the $this->_children variable after the layout has processed, I get a list of elements that includes mine.

However, it's not rendering to the page and I've not idea what else I need to do output it.

My blocks are loaded programatically, i.e. without a layout or template file.

Best Answer

In order to view a block in the layout it fit in one of the 2 cases:

  • it has a _toHtml method that returns something
  • it extends Mage_Core_Block_Template and has a valid template associated.
Related Topic