Sometimes you want to call a block inside a .phtml file without defining in layout. In this case, we can use a method called toHtml of a block.

Assume that we have a block called Mymodule and a template that is located at: mymodule/myblog.phtml

So to generate that block we use:

<?php echo $this->getLayout()->createBlock('mymodule/myblock')->setTemplate('mymodule/myblock.phtml')->toHtml(); ?>

Related Tutorials:



Why Magestore? We believe in building a meaningful & long-term relationship with you.


  1. Oh, it’s really useful. It helps me too much in my current project.
    Thank u so much!

  2. I tried to use the:

    getLayout()->createBlock(‘catalog/related’)->setTemplate(‘product/list/related.phtml’)->toHtml(); ?>

    To call the related product to my basket cart but it doesn’t work, any one have any idea why?

  3. you called wrong block and template. You maybe should try:
    getLayout()->createBlock(‘catalog/product/related’)->setTemplate(‘catalog/product/list/related.phtml’)->toHtml(); ?>
    Please make sure you have block Related and file related.phtml in correlative directories.

  4. Vishal Lakhani Reply

    After Spent whole day with headache found solution here.. Thank you very Much

  5. I got this to work by doing the following: Create 5 new sittac blocks, each named featured_product_# where the # was 1 through 5. Put the html code from the example above to point to my banners folder and saved each page. Did not touch the cms.xml file. Instead I took the php code provided above and went into my /template/page/html/ folder and edited the header.phtml where I wanted the 5 random banners to display on the top right of my site by my logo. I would imagine if you wanted them to only display on the product details page you’d go into /template/catalog/product folder and edit the view.phtml and place the php there. Hope this helps others. Works like a charm. Thanks. Running version

  6. If we call any .phtml file directly it will effect any performance issue

Write A Comment