5

What is the difference between below two pieces of code?

Code 1

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


Code 2

$block = $this->getLayout()->createBlock('core/template')->setTemplate('PATH/RELATIVE/TO/TEMPLATE/*.phtml')->toHtml();
$this->getResponse()->setBody($block);
Anshu Mishra
  • 8,950
  • 7
  • 40
  • 88

2 Answers2

14

Splitting this apart

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

This

$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

$block->setTemplate('PATH/RELATIVE/TO/TEMPLATE/*.phtml');

Will set a variable named template on the Mage_Core_Block_Template object.

then this

$block->toHtml();

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();

This

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

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

$block->toHtml();

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.

Alana Storm
  • 44,345
  • 35
  • 168
  • 353
1

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

dagfr
  • 236
  • 1
  • 10