rss
twitter
    Find out what I'm doing, Follow Me :)

Tuesday, April 27, 2010

Pin It

Widgets

Creating Block From the Code in Magento


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.

// Directly in the PHTML where you want to show the block
<?php
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.
// In layout
<block type="module/block" name="module.name" template="module/page.html"/>
<?php
// In parent PHTML
$this->getChildHtml("module.name");
?>

No comments:

Post a Comment