Programatically create Magento blocks and inject them into layout
11 Comments 12th JUN 2009 | Posted by Branko Ajzele in Magento

Imagine a scenario where you wish to simply create a view file like custom-note.phtml and show this view file on some new url inside the Magento store. One way to do this is to create a CMS page and call that block from within CMS page. But what if you wish to create and append this block to other areas of Magento layout that are inaccessible from CMS page interface form admin? What if I want to add new div element under the breadcrumbs and append new block under it?
magento philosophy is to create block class under the /Block folder of your module, to create xml layout file under the theme /layouts folder (or to modify existing layout file) and so on. Anyway you turn it around you either need to have Block file or add/modify at least the entry to the layout files.
All this is OK if you are working on your own store so you have full control over the code. However, what if you are developing a module that will be used on different Magento stores. My prime concern when building a module is to keep the number of necessary module files to a minimum.
In code below, you will see how easy it is to call your Core/Template block to show on any area of Magento layout.
Extracted from app/code/local/ActiveCodeline/CustomOutputs/controllers/IndexController.php file.
public function indexAction()
{
//Get current layout state
$this->loadLayout();
$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'my_block_name_here',
array('template' => 'activecodeline/developer.phtml')
);
$this->getLayout()->getBlock('content')->append($block);
//Release layout stream... lol... sounds fancy
$this->renderLayout();
}
Most IMPORTANT thing to keep in mind here is the “error handling”. If you assign a “invalid” block to ->append(), you will not (most likely) see an error but “nothing happend” situation.
Anyhow… hope the attached module (extension) will save you some headaches. I know it took me few hours of tracing and testing to get the grip of it.
Download ActiveCodeline_CustomOutputs extension.
Cheers.
To post code in comments, place your code inside [code] and [/code] tags.


















June 12th, 2009 at 15:06
Another example on how to space the block to any area of screen
$this->getLayout()->getBlock(‘right’)->insert($block, ‘catalog.compare.sidebar’, true);
This will position it on right sidebar under the ‘catalog.compare.sidebar’ block.
July 12th, 2009 at 6:32
Thanks for another helpful post.
The code above can also be written as follows:
$block = $this->getLayout()->createBlock(
‘core/template’,
‘my_block_name_here’)
)->setTemplate(‘activecodeline/developer.phtml’);
Is it not better to load the Block using it’s “alias name”, so that in case it gets overridden by another module, the overridden class will be loaded?
July 12th, 2009 at 6:43
The “createBlock” method is used in quite a few controllers throughout the Magento codebase. It’s especially used in the Adminhtml module. There are many places where a controller loads a block using the createBlock method, where that could have also been specified in a layout XML file. The createBlock method is essentially a more direct way of loading a block as opposed to typing up the XML for that block in a layout file.
When you see a … in a layout file, you should know that it ultimately gets loaded by the Mage_Core_Model_Layout class, the same object type that gets returned by $this->getLayout()
July 12th, 2009 at 6:47
This comment box doesn’t like XML tags – it replaced some XML tags from my previous post with ….
Here’s the XML I posted:
<block type="**/****">…</block>
July 22nd, 2009 at 7:25
thanks !it is helpful for me
January 13th, 2010 at 9:02
Wonderfull, just what I was looking for simplest module possible. Thx!
February 12th, 2010 at 16:12
Can i add a custom template to my backend ?
i want print my orders in html format and i need to create a block under adminhtml
March 3rd, 2010 at 20:05
Can you create block in the xml layout file. For example, I want to use X block
X block name
X – new block that needs to be created, but how to do it in layout xml file?
Simply putting following will not work
March 16th, 2010 at 11:01
Hi,
Your post is very helpful
Thanks,
March 30th, 2010 at 5:53
Hi,
I want to add newsletter block into my one column products page. How to add newsletter block in desired location of the page.
Thanks
July 20th, 2010 at 18:50
Hi, I’m using this piece of code to create a simple contact form, but it appears with a default 3 columns layout and I’d like to know how can I set it to appear with just 1 column.