Programatically create Magento blocks and inject them into layout

Featured Image

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.

33
Top

Care to rate this post?

Author

Branko Ajzele

Ex Inchooer

Worked at Inchoo as a Backend Developer/CTO from 2008 to 2013

Other posts from this author

Discussion 33 Comments

Add Comment
  1. 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.

  2. 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?

  3. 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()

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

  5. thanks !it is helpful for me

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

  7. mzouhel

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

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

  9. Tejas

    Hi,

    Your post is very helpful :)

    Thanks,

  10. Daisy

    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

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

  12. And my question is how can I access layout object from inside some block. I want to get current page title (if its set already).

  13. Fabio

    Hi.
    I am trying to work my ways trough magento and i can’t find a way to create a block the “right” way.
    Can you point me to the doc or a tutorial of how to create a block with just the necessary files?

  14. I don’t know what you are using for code formatting but at least on my MFC-440CN it just prints out garbage instead of the code. :(

  15. Oleg Kudrenko

    Hi!
    I need to create many cms page programmatically. How can i do it, what methods i must use?

  16. Use this for set layout of page

    $template = Mage::getConfig()->getNode(‘global/page/layouts/two_columns_right/template’);
    $this->getLayout()->getBlock(‘root’)->setTemplate($template);

  17. neeraj

    thanks Salik

  18. Harry

    I dont want to use the core/template as a block type.. what if i want to use checkout/cart… i have tried the following code but its is not working….

     public function indexAction()
    		{
    			//Get current layout state
    			$this->loadLayout();
    			$block = $this->getLayout()->createBlock(
    				'checkout/cart', // OR 'Mage_Checkout_Block_Cart'
    				'my_block_name_here',
    				array('template' => 'custom/cart.phtml')
    			);
    			$this->getLayout()->getBlock('content')->append($block);
    			$this->getLayout()->getBlock('root')->setTemplate('page/empty.phtml');
    			//Release layout stream
    			$this->renderLayout();
    		}
    

    any suggestions will be highly appreciated…. Thanks you.

  19. haifa

    thanks for this! helped me a lot!

  20. Rao

    how can i create dynamic breadcrumbs for my custom module. i am in 80% of my first magento module construction and this is what i am on right now. kindly guide me.
    elegant.rao@gmail.com
    thank you.

  21. I would also like to know how to create dynamic breadcrumbs for pages that don’t have them. Thanks.

  22. Hello,

    I am trying add one or more field when Export product grid to XML format.

    public function exportXmlAction()
    {
    $fileName = ‘products.xml’;
    $content = $this->getLayout()->createBlock(‘enhancedgrid/catalog_product_grid’)
    ->getXml();

    $this->_sendUploadResponse($fileName, $content);
    }

    I dont get idea how and where I have to change.

    I wand add URL field in exported XML file…

    please help me…

    thanks
    Praful

  23. Samit Rimal

    It is not working in magento 1.6.2 . How to solve it

  24. hii thanks for this helpful post, it is very helpful for me, bu i m having a problem when i m using it ,i cant access the functions in my block Mage_CatalogSearch_Block_Search a i have created a per your code where u have used Mage_Core_Block_Template class, can i use another class overhere?
    if yes then i have done it but it is giving me error pls send me reply asap

  25. Jignesh Thummar

    @Branko Ajzele . its possible to inject block in default layout without using layout xml file? because this approach add block to current controller action but in case if i want something to inject in header of all the pages possible without layout xml?

  26. vimal

    hey Branko Ajzele ,thanks for your post .tell me one thing how to make this block available in all pages ?

  27. Rao

    @scott,
    i know this is late but heres how i did it regarding creating dynamic magento breadcrumbs for custom module.

    /* the main title will be appended by the categories meta titles */
    $breadcrumbs = $this->getLayout()->getBlock(‘breadcrumbs’);
    /* calling breadcrums block */
    $breadcrumbs->addCrumb(‘home’, array(
    ‘label’ => Mage::helper(‘cms’)->__(‘Home’),
    ‘title’ => Mage::helper(‘cms’)->__(‘Home Page’),
    ‘link’ => Mage::getBaseUrl()
    ));

  28. Using the code from your indexAction. For some reason my header isn’t loading. So I have only the content block and then footer block. Here is my code, very similar to your example:

    public function indexAction()
    {
    //Get current layout state
    $this->loadLayout();
    $block = $this->getLayout()->createBlock(
    ‘Mage_Core_Block_Template’,
    ‘service-form’,
    array(‘template’ => ‘optimise/service/form.phtml’)
    );
    $this->getLayout()->getBlock(‘content’)->append($block);
    $this->renderLayout();
    }

    Any idea why my header isn’t loading?

  29. Using the code from your indexAction. For some reason my header isn’t loading. So I have only the content block and then footer block. Here is my code, very similar to your example:

    public function indexAction()
      {
      //Get current layout state
        $this->loadLayout();
        $block = $this->getLayout()->createBlock(
          'Mage_Core_Block_Template',
          'service-form',
          array('template' => 'optimise/service/form.phtml')
          );
        $this->getLayout()->getBlock('content')->append($block);
        $this->renderLayout();
      }
    

    Any idea why my header isn’t loading?

  30. Hi colleagues, its great paragraph regarding educationand completely defined,
    keep it up all the time.

  31. Awesome blog! Do you have any hints for aspiring writers?
    I’m planning to start my own website soon but I’m a little
    lost on everything. Would you recommend starting with a free platform like
    Wordpress or go for a paid option? There are so many options out there that I’m totally confused .. Any ideas? Appreciate it!

  32. controller

    $this->loadlayout();
    $block = $this->getLayout()->createBlock(
    ‘catalog/product_list’,
    ‘catalog.product.list’,
    array(‘template’ => ‘catalog/product/list.phtml’)
    );
    $block->setAttributeToFilter(‘is_rentable’, 1);
    //echo $block->toHtml();
    $this->getLayout()->getBlock(‘content’)->append($block);

    $this->renderLayout();

    public function setAttributeToFilter($attribute_code, $value)
    {
    $this->_getProductCollection()->addAttributeToFilter($attribute_code, array(‘eq’ => $value));
    return $this;
    }

  33. dishank

    how can i add dynamic block into my home page???

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top