I have following XML:
<reference name="content">
<block type="name/myblockname" name="blockname" alias="blockalias" template="name/myblockname.phtml">
<action method="setData"><key>name</key><value>value</value></action>
<block type="core/template" name="otherblock" template="catalog/product/view/otherblock.phtml"/>
</block>
</reference>
And I need same as code in block constructor, I tried this. Result was that block was rendered, but getChild inside name/myblockname.phtml returns null value and not the block.
class MyCompany_ModuleName_Block_MyBlock extends Mage_Core_Block_Template
{
public function __construct() {
parent::__construct();
// $layout = $this->getLayout(); // this didn't work
// $layout = Mage::getModel('core/layout'); // this didn't work
$layout = $this->loadLayout()->getLayout(); // this didn't work
$block = $layout->createBlock("core/template");
$block->setTemplate("catalog/product/view/otherblock.phtml");
$block->setNameInLayout("otherblock");
$this->append($block, "otherblock");
}
// ... other stuff here ...
}
Here is how I am including my block to CMS page:
{{block type="name/myblockname" name="value" template="name/myblockname.phtml"}}
I want to know what I am doing wrong or is this even possible in Magento? (Don’t worry about xml etc. namings, I had to overwrite those, because they contained company data, so they could be wrong in this example, but most likely are not in original code.)
I highly recommend to use the “fake”-constructor
_construct(), which is called by the core__construct()to avoid misusage.Then to add a new child block use this code:
Then in the block’s phtml file, you call the child by: