In Magento, block is a View element of MVC (model-view-controller), having the major task to make data displayed to the users. While creating a theme, you are offered many content blocks that are placed in the structural block. If you are not sure about it, please read my article today which partly helps you understand the block structure along with the relationship between templates and blocks.1. The structure of block
All of blocks in Magento are inherited from Mage_Core_Block_Abstract. Blocks requiring template files to be displayed are inherited from Mage_Core_Block_Template.
A general block includes some components as below:
- Code file to get data information
- Functions to work with layout, parent blocks and children blocks
- Template file to render to html
2. The relationship between template and block
A block will be shown by a template file. When a block returns to HTML code by calling toHtml() function, it will get a template file and then flush variables to this file. One block can flush its variables many template files. Similarly, it’s possible for a template to show lots of block types.
A template is maybe unnecessary for one block but one template compulsorily requires a block attached to it in Magento. In a template file, we are able to use $this pointer to call functions as well as take the values of the block attached to this template.
That’s all. I hope this article will help someone 😀