Here are some methods of creating block by code in Magento. This type of code can be used directly in template, not need to create a layout file or add a child node inside already existing layout file. You can use this to echo a different type of block inside already loading block and template.
1 |
// Directly in the PHTML where you want to show the block |
3 |
echo $this->getLayout()->createBlock("module/block")->setTemplate("module/page.phtml")->toHtml(); |
What we have actually done is just load the layout and create an instance of a block class defined inside its paramerter, then assigned a template to the block and finally echoed as html. Simple! The code above will be equivalent to what we do as following in layout and in template file.
2 |
<block type="module/block" name="module.name" template="module/page.html"/> |
3 |
$this->getChildHtml("module.name"); |
May be helpful!
Filed under - how-to, Magento, php, tips and technique
Tags: Block, how-to, Import, Layout, Magento, Map Type, tricks