Describe the programmatic structure of blocks in Magento
What are blocks used for in Magento?
Blocks are the main design element in Magento. A hierarchy of blocks is used to layout any page in a Magento store. Blocks in Magento divide into two big groups:
- Structural Blocks
- Content Blocks
Programmatically there are no difference between them, but structural blocks are use in Magento to define main areas of a page, while content blocks are used to render a particular area of it.
For example, in a typical home page of a Magento Store you may see a (simplified) hierarchy similar to the following:
- Navigation menu
- Main Banner
- Latest Products
- (Sidebar) Left
- Layered navigation
- Promo banner
- Terms and Conditions
Its a very simplified layout and the name are not as it should be in a real store, but Header, Content, Sidebar and Footer would be structural blocks, and everything down them would be content blocks.
What is the parent block for all Magento blocks?
All Magento blocks inherits
Mage_Core_Block_Abstract that inherits directly from
Which class does each block that uses a template extend?
Any Magento Block that uses a template file, inherits from
Mage_Core_Block_Template which inherits from
In which way does a template block store information about its template file?
A Magento template block stores the relative path to the template file in a protected variable
_template that can be accessed to the corresponding setter and getter methods (
The stored path begins at the template folder but it doesn´t store information about the package or theme to be used. That is determined in Magento during the render phase, by the
For example, the full template path for the cart block is:
The corresponding block will store only the last part of the path:
Does it store an absolute or a relative path to the template?
As told in the previous section, the path to a template file is stored in Magento in a relative way, without any information about the package or theme to be used.
What is the role of the Mage_Core_Block_Abstract class?
Mage_Core_Block_Abstract is the base class for all blocks in a Magento store and it provides basic functionallity for working with layout, block hierarchy (insert, remove childs…), output contents, and so on.
Hi Daniel, Thank you for the article.
what is the difference between rendering a layout and loading a layout? or rendering a block and loading a block?
we have two functions which controller uses renderlayout and LoadLayout.
They do completely different things indeed.
During the loadLayout call, magento looks in the layout xml files and gets a list of blocks that will be used during the current request. For example if the current request is the cart page, the list of blocks will include the header, footer, cart items, totals, and so on, but it won’t include others blocks like category view, contact form, etc. That is, loading a layout gets a list of the needed blocks.
Also, at the end of the load layout stage, magento creates an instance of each of this blocks and applies the «actions» defined in the layout xml.
One all of these is done, reder layout gets the output (html). The render stage starts with a call to the toHtml method of the root block, and then, all of its childs, and childs of childs, and so on. At the end of the render stage, the generated output is sent to the browser.
Thank you Daniel! great help!