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.

Interested in hiring us?

Have a chat with us. You would be surprised how small changes can make your business even more successful.


33 comments

  1. 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;
    }

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

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

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

  5. @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()
    ));

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

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

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

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

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

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

  12. Use this for set layout of page

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

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

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

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

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

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

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

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

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

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.