In this blog, we are going to learn Magento blocks.
What Are Blocks in Magento?
Block is first class elements in the structure of Magento layouts, in Magento architecture. Blocks are powerful and flexible way to plug your content into already existing layouts. You can remove or reposition existing blocks by using XML files very easily. You can also create your own blocks using custom module in Magento. For example: You want to display special products for upcoming season on the front page then you can achieve it by implementing custom blocks.
Blocks are of two types:
Structural Blocks and Content Blocks
Structural Blocks are the containers for holding the "Content Blocks". They don't have any actual content to display but in the end they turn to display the content blocks. They are used to structure the content of the web page. For example, "Header", "Footer", "Left" and "Right" are structural blocks in Magento.
Content Blocks are used to display the actual content for displaying it on the web page. Majority of contents are generated by the content blocks for different modules.
Custom Block Development
Let's begin to develop custom block for our custom Magento module. It will be a simple block which will be used to display most recent products available in the store.
These are the file structures for our custom block that will be needed to implement
app/etc/modules/Envato_All.xml: It's a file used to enable our custom module.
app/code/local/Envato/Recentproducts/etc/config.xml: It's a module configuration file.
app/code/local/Envato/Recentproducts/Model/Recentproducts.php: It's a model file which provides the array of most recent products.
app/code/local/Envato/Recentproducts/Block/Recentproducts.php: It's the main block file for our custom block.
app/design/frontend/default/default/template/recentproducts/recentproducts.phtml: It's a template file which contains the XHTML-related stuff.
Setting Up the Files
First we need to create the module enabler file. Create app/etc/modules/Envato_All.xml and paste the following contents into that file. We've used Envato as our module namespace and Recentproducts as our module name.
Create app/code/local/Envato/Recentproducts/etc/config.xml and paste the following contents in that file. We've just declared the model and block class names as per the Magento module XML file conventions.