Programatically create Magento blocks and inject them into layout

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.

You made it all the way down here so you must have enjoyed this post! You may also like:

How To Connect Google Analytics 4 To Magento 2 Bojan Mareljic
Bojan Mareljic, | 36

How To Connect Google Analytics 4 To Magento 2

Programmatically add a custom block in Magento Admin Kristijan Glibo
Kristijan Glibo, | 4

Programmatically add a custom block in Magento Admin

Layout of your custom controller in Magento? Vedran Subotic
Vedran Subotic, | 12

Layout of your custom controller in Magento?

35 comments

  1. hey please tell me the alternate option for that or how i can find solution if i get nothing in page.

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

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

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

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

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

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

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

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

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

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

  13. Use this for set layout of page

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

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

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

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

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

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

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

  20. 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 🙁

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

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

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

  24. 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> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.