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.