Hi, in this post, we will understand the concept of blocks in Magento.

Magento blocks are bascially classes that contains methods that can be accessed in their corresponding views. Every page in Magento is created using layouts and it is filled by blocks.

There are two type of blocks in Magento:

Structural Blocks

Structural blocks are also called container blocks. They help in dividing your page into sections like header, footer, left column, right column etc. They are also parent of content blocks.

Content Blocks

Content blocks generates the actual content to display on the page. They are part of structure blocks i.e. every content block belongs to a structural block.

Blocks are declared in the layout files with the following statement type:

<block type="module_name/class_name" name="some_name" as="alias_name" template="path_to_phtml"/>

Example 1:

<block type="page/html_notices" name="global_notices" as="global_notices" template="page/html/notices.phtml" />

In the above statement, page is the name of the module ( you can check Page folder in app\code\core\Mage ) folder. Next, there will be a file Notices.php under Block\Html folder. If your file is inside another folder under block, put an underscore.

Example 2:

<block type="page/html" name="root" template="page/print.phtml">

In this statement, Page is a folder under app\code\core\Mage. Then there will be a file Html.php under the Block folder. Now, the template file we assigned to this block is print.phtml under page folder.

Blocks can also be nested. Here is an example:

<block type="page/html_header" name="header" as="header">
    <block type="page/template_links" name="top.links" as="topLinks"/>

Attributes of the <block> tag

  1. type
    This is the identifier of the module class that defines the functionality of the block.
  2. name
    This is the name by which other blocks can make reference to the block in which this attribute is assigned
  3. before/after
    These are used to position a content related to existing structural block
  4. template
    Tempalte (phtml) file to display in the block
  5. action
    This is used to control store-front functionalities such as loading or unloading of a Javascript.
  6. as
    This is the name ( alias ) by which a template calls the block in which this attribute is assigned. When you see the statement inside a template:


    you can be sure it is referring to the block whose attribute as is assigned the name block_name.

Setting template in the code
Instead of specifying template file in the <block> tag, we can also specify it in the class file. Here is an example:

class Test_Basic_Block_Model extends Mage_Core_Block_Abstract
    public function __construct()

Auto-rendered blocks

These are the blocks that are rendered automatically. We don’t have to call getChildHtml() for such blocks. Block type core/text and core/text_list are automatically rendered.


<reference name="content">
   <block type="core/text_list">
       <block type="basic/news" template="custom/news.phtml"/>

reference is used to refer an existing block. Here we are referring to an existing block named content and adding a new block to it.

Manual blocks

All other blocks are manual blocks and must be rendered by calling getChildHtml(‘block_alias’) method.

Creating blocks in Magento