Programatically create Magento blocks and inject them into layout

11 Comments 12th JUN 2009 | Posted by Branko Ajzele in Magento

Programatically create Magento blocks and inject them into layout

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.

If you like what you read, please share it.

  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • Yahoo! Bookmarks
  • Reddit
  • Technorati
  • Twitter
  • StumbleUpon
  • LinkedIn
  • Netvibes
  • NewsVine
  • Sphinn
  • Tumblr
  • Posterous

To post code in comments, place your code inside [code] and [/code] tags.

There are 11 comments (Add Yours +)

  • 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.

  • 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?

  • 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()

  • 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>

  • thanks !it is helpful for me

  • Wonderfull, just what I was looking for simplest module possible. Thx!

  • 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 :(

  • 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

  • Hi,

    Your post is very helpful :)

    Thanks,

  • 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

  • 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.

Leave a Comment

Please wrap all source codes with [code][/code] tags.
Magento Design and Development | Magento SEO | iPhone Application Development Web Application Development with ZEND | WordPress Ecommerce | WordPress development
Sitemap

Inchoo - webappsolutions | 2009