Magento Unique Blocks

In this blog post we are going to see some unique blocks in magento which are different from all other standard magento blocks.

Most blocks in magento extend the Mage_Core_Block_Template class, but there are few other block which don’t extend this class. Due this reason these blocks don’t have a template file and behave differently.

Mage_Core_Block_Text_List

This block is called a Structural Blocks in magento. This block doesn’t have any template file and has its own custom “_toHtml()” function.
The sole purpose of this block is to simply display output of its child blocks in a sorted order and display it.

    protected function _toHtml()
    {
        $this->setText('');
        foreach ($this->getSortedChildren() as $name) {
            $block = $this->getLayout()->getBlock($name);
            if (!$block) {
                Mage::throwException(Mage::helper('core')->__('Invalid block: %s', $name));
            }
            $this->addText($block->toHtml());
        }
        return parent::_toHtml();
    }

As you can see above it reads all its child blocks in a sorted manner and displays their output.
This block is very useful and is used through magento. Most structural blocks like “content”, “right”, “left” etc are all of type “core/text_list”. This is the reason why we are able to take “reference” of blocks like “content”, “right”, “left” and add child blocks to it, and these block automatically get displayed.

Usage

<block type="core/text_list" name="right" as="right" translate="label">
                <label>Right Column</label>
            </block>

Mage_Page_Block_Html_Wrapper

This is another important block in magento. This block creates a wrapper element and appends all block output inside it. Wrapper basically means creates a >div< by default and put html inside it.

Usage:

               <block type="page/html_wrapper" name="top.container" as="topContainer" translate="label">
                    <label>Page Header</label>
                    <action method="setElementClass"><value>top-container</value></action>
                    <action method="setElementId"><value>top-container</value></action>
                    <action method="setElementTagName"><value>div</value></action>
                </block>

By default the element tag name is ‘div’, but we can use any tag name

Mage_Core_Block_Text

This is another block which is not used very often. The class “core/text_list” extends “core/text” class.
This block has a very simple purpose, we can add any text to it and it will display it.

   <block type='core/text' name='test.text'>
      <action method="addText" translate="text"><text>Hello Text</text></action>
      <action method="addText" translate="text"><text>Before Hello!</text><before>true</before></action>
   </block>

This block is seldom used in magento.

Mage_Core_Block_Flush

This block is same as the ‘core/text_list’ block in its function except it has ” ob_implicit_flush();” function called in its “_toHtml()” function. This basically turns off output buffering which is not good since magento by default use output buffering to render its template files.
I could not find where this block is used in magento right now. Its also mentioned in comments Immediate flush block. To be used only as root

Usage:


Mage_Core_Block_Messages

This block is used in magento to display all error/success/warning messages. The “_toHtml()” function basically reads all error/warning/success from core/session class and display all of them together.

Usage

Mage_Core_Block_Text_Tag

This is a block which can be used to generate any html tag.

Usage:

            <block type='core/text_tag' name='test.tag'>
                <action method='setTagContents'>
                    <text>Tag Content</text>
                </action>
                <action method='setTagName'>
                    <value>a</value>
                </action>
                <action method='setTagParam'>
                    <param>href</param>
                    <value>http://google.com</value>
                </action>
            </block>

We also have other classes “Mage_Core_Block_Text_List_Item” and “Mage_Core_Block_Text_List_Link” which are used for similar purpose.

Mage_Core_Block_Profiler

This blocks as the name suggests is used to display the profiling information.