What is the difference between below two pieces of code?

Code 1

$block = $this->getLayout()->createBlock('CLASS_GROUP_NAME/PATH_TO_BLOCK_FILE')->toHtml();

Code 2

$block = $this->getLayout()->createBlock('core/template')->setTemplate('PATH/RELATIVE/TO/TEMPLATE/*.phtml')->toHtml();
  • Looks like an error to me? I don't think setTemplate should have a class, it should have a file name ending with .phtml
    – philwinkle
    Commented Aug 6, 2013 at 17:30
  • Thanks for pointing out, I have changed that to some path to phtml file. Commented Aug 6, 2013 at 17:46

2 Answers 2


Splitting this apart

$block = $this->getLayout()->createBlock('core/template')->setTemplate('PATH/RELATIVE/TO/TEMPLATE/*.phtml')->toHtml();


$block = $this->getLayout()->createBlock('core/template');

instantiates a Mage_Core_Block_Template object. The class name comes from looking in the merged config.xml for a node inside <blocks/> named <core/>, and then appending the word Template.

then this


Will set a variable named template on the Mage_Core_Block_Template object.

then this


will render the block as HTML. It just so happens a Mage_Core_Block_Template block's toHtml method renders the block by loading a phtml template set in the template variable.

Then, splitting apart this

$block = $this->getLayout()->createBlock('CLASS_GROUP_NAME/PATH_TO_BLOCK_FILE')->toHtml();



instantiates a block object. It gets the base class name from looking in the configuration for a node named <class_group_name/> under the <blocks/> configuration node. (for this example, let's say that's Packagename_Modulename_Block) — and then appending PATH_TO_BLOCK_FILE to the end of this. This is the same process as with core/template above. This gives us a block named Packagename_Modulename_Block_Path_To_Block_File.

Then this


calls the Packagename_Modulename_Block_Path_To_Block_File's object's toHtml method, which renders the block. It maybe Packagename_Modulename_Block_Path_To_Block_File extends from Mage_Core_Block_Template, and has had it's template set inside itself, or it may be a block that contains different logic for rendering in toHtml.

  • Thanks a lot, This is really very good and helpful for me. Commented Aug 11, 2013 at 7:28
  • "The class name comes from looking in the merged config.xml" Where is this file?
    – Black
    Commented May 23, 2018 at 11:22
  • 1
    @Black Every module has a config.xml. Magento merges these with PHP code, and stores the merged tree in its cache. There is no file that represents a merged XML tree. Commented May 23, 2018 at 15:30

This is quite the same method. The difference are :

CASE 1 This call uses a specific block, with its own method and feature (i.e load collection, set caching, etc.). It may have a default template set so you don't need to specify it.

CASE 2 This call uses the default frontend block, YOU NEED to define the template to use. You only have default block feature in this one (see the mentionned class to find more info).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.