:::: MENU ::::

Magento Certification: Rendering Blocks (part 5)

4.40 avg. rating (84% score) - 5 votes

Identify different types of blocks in Magento

In this article we will check for the main characteristics of some of the pre-defined blocks in Magento.

What is the purpose of each of the following block types

  • Mage_Core_Block_Template
  • Mage_Core_Block_Text_List
  • Mage_Core_Block_Text

Mage_Core_Block_Template

It is for sure the most used block in Magento.

The most important feature of this type of block is of course that you can asign it a template file for rendering. That template will be a file stored on the theme level, so we could say this block is maybe the base block for Magento support of themes.

When the block it’s going to be rendered, Magento looks for the template file, first in the active theme, then in the default theme of the active package and finally in the base theme.

This block, as the others, may have child blocks nested. Child blocks are not automatically rendered but by calling getChildHtml from the template file.

The template file has access to all methods and data from the block, just by calling $this, as the template file is ‘included’ during the rendering phase.

public function fetchView($fileName) {
    . . .
    // Templates can be output directly to the browser or its content added to 
    // the response which at the end of the process is sent to the browser 
    // in one piece. In most cases, it will be stored in the response and 
    // Magento will send it at the end of the request processing
    $do = $this->getDirectOutput(); if (!$do) { ob_start(); }
    . . . 
    // This part includes the template file directly, so it's result is output 
    // directly or captured to a variable (see ob_start and ob_get_clean calls) 
    // The template file path is generated by realpath... 
    $includeFilePath = realpath($this->_viewDir . DS . $fileName); 
    if (strpos($includeFilePath, realpath($this->_viewDir)) === 0 || $this->_getAllowSymlinks()) { 
        include $includeFilePath;
    }
    else {
        Mage::log('Not valid template file:'.$fileName, Zend_Log::CRIT, null, null, true);
    }
    . . . 
    // Finally the generated output by the template, is captured or output.
    if (!$do) {
        $html = ob_get_clean();
    }
    else {
        $html = '';
    }
    . . . 
}

Mage_Core_Block_Text

This is the most simple block in Magento.

It inherints directly from Mage_Core_Block_Abstract and extends its base class only with three methods:

  • public function setText($text)
  • public function addText($text, $before=false) {}
  • public function getText()

The block only stores a text string (it could be plain or html of course), and during rendering phase, the block simply outputs the stored string.

You could set the string to a value or append / prepend the text to the existing string.

Mage_Core_Block_Text_List

This blocks inherits fromt the previous one, but it’s function is completely different.

It is some kind of container that stores child blocks. It has no own data nor template file, and it outputs nothing by itself.

You need to assign childs to the block by either the xml layout files or by calling some of the methods for that: append, insert, and so on (defined in Mage_Core_Block_Abstract).

To add a child block in a layout file:

<block type="core/text_list" name="name-of-the-parent"> <block type="module/block" name="name-of-the-child" template="some/template/file.phtml" before="-"/> </block>

To add a child block programmatically:

$block = $this->getLayout()
              ->createBlock('core/template')
              ->setTemplate('somefolder/sometemplate.phtml'); 
$parent->append($block, 'some-alias');

When rendering Mage_Core_Block_Text_List, Magento renders all the children one after the other. The order of the output is defined by assigning two parameters after and before to each block.

Which block type renders its children automatically?

As seen before, Mage_Core_Block_Text_List do.

Which block type is usually used for a content block on Magento pages?

The content block (as the left and right columns) is defined in Magento as Mage_Core_Block_Text_List, so you can add blocks to the content area without needing to modify a template file.
NOTE: As Simon mention in the comments below, this is not the correct solution to the question. Blocks used to generate content in Magento are usually inherited from Mage_Core_Block_Template.




2 Comments

  • Responder Simon |

    Hi Daniel,

    first: thanks for the enlightening overview, but I fear there is some wrong information in the last paragraph. «Content» block dows – in the Magento-wording – not refer to a block named «content», but to a type of block opposed to «structural» blocks.

    core/text_list is a structural block, meaning it has no content in himself and merely renders some child-blocks – which is why they are rendered automatically. Mage_Core_Block_Template is a content block, meaning it actually has content.

    I am letting this here for future googlers. :)

Hey! Qué opinas sobre el artículo?