What is magento blocks/ Magento block architecture
What is magento Block
Block: Magento Block is actually a part of code which is reponsible to communicate with database model and make that available to layouts phtml file. Actually blocks are the small component of page render which take specific position on page. In other words blocks are not like complete page but its a specfic part of page .
If you want call any block on any view page (.phtml page) then you need to call blocks through their congig.xml file. Under your config.xml file you need to find out the location where you need to call specific block and then you need to call that block by writing code there.
The typical way to call a block from config.xml file is :
Example 1: <block type="page/html_notices" name="global_notices" as="global_notices" template="page/html/notices.phtml" />
Example
2:
<reference
name="
header
">
<block
type="core/template"
name="test"
template="at.phtml"></block>
</reference>
These reference name are the reference of area of page where these blocks to be placed.
These references are written as like below code:
<block type="page/html_header" name="header" as="header">
To call these blocks
on phtml files we need to write code like
<?php echo $this->getChildHtml('global_notices') ?>
You can create blocks through your controller file as well. Please go through http://www.cutehits.com/2011/07/creating-block-from-controller-in-magento/ for Creating Block from controller in Magento
Magento Block Type:
In order to call a block we need to pass block type . Block type always written like A/B (like page/html_header
)
where
A: it is module alias. You can see “page” as alias is defined on app\code\core\Mage\Page\etc/config.xml. If you will open this file then you will find that
<blocks>
<page>
<class>Mage_Page_Block</class>
</page>
</blocks>
through this you can see “page” is the alias for block where class name is “Mage_Page_Block”. In Magento class name explain location of file.
B: is the class name relative to alias. From the above example of “page/html_header” you can see B part is html_header. So the actual path of this block would be Mage_Page_Block_Html_Header and as per the naming convention this block would be availabe at app\code\core\Mage\Page\Block\Html\Header.php. This PHP file actually contain all the functions which is been used by block template file.
How to setup Block template path in magento:
*. Block template path is written either directly under <block tag for example:
<block type=”page/html” name=”root” output=”toHtml” template=”page/3columns.phtml”>
Or
You can also set this from your block php file like
protected function _construct()
{
$this->setTemplate(‘page/html/head.phtml’);
$this->setData(‘is_enterprise’, Mage::getEdition() == Mage::EDITION_ENTERPRISE);
}
Commonely Used Magento’s built in blocks
*. core/template : This block renders the template called by template attribute.
*. page/html: This block is basically subtype of core/template . It is defined as root block almost all other blocks are child block of this one.
*. page/html_head : It defines html head section of page which contains element like css javascript etc
*. page/html_header : This block defines header part of page like site logo, top links etc.
*. page/template_links : Basically it used to create a list of links . These links are visible to footer header and other sections of page.
*. core/text_list : There are some blocks like content, left, right are actually a type of core/text_list. When these blocks are rendered then all of their internal blocks get rendered automatically without writing getchildhtml() for each.
*. page/html_wrapper : It used to create wrapper block on magento page which render its child block inside an HTML tag that is set by an action setHtmlTagName
.
*. page/html_breadcrumbs : this block is totally responsible for handling breadcrumb of page.
*. page/html_footer : This block contain footer area of page including copyright message
*. core/messages : This block hander all messages including success/error/ validation/ warning etc.
*. page/switch : This block shows store language switcher options on site based on the location where its been called.
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments