The Programmatic Structure of Blocks in Magento 1 and 2

The Programmatic Structure of Blocks in Magento 1 and 2

What are blocks used for in Magento?

Blocks in both Magento versions 1 and 2 are divided into two types:

  • Structural blocks
  • Content blocks

In Magento 2 containers which allow separating structural and content blocks have been added. The key distinction between containers and blocks is that containers don’t use phtml templates, but keep the set of content blocks. While content blocks have their own templates and are rendered on the page within containers.

Here is the example:

As you can see, we place the block checkout.cart.top.step with the template Magento_Checkout::stepcheckout.phtml within the container checkout.cart.top.div. Consequently, when containers output all blocks the container keeps will be displayed.

What is the parent block for all Magento blocks?

In Magento 1 all blocks are inherited from Mage_Core_Block_Abstract.
In Magento 2 all blocks are inherited from Magento\Framework\View\Element\AbstractBlock.

Which class does each block that uses a template extend?

In Magento 1 all blocks using templates are inherited from Mage_Core_Block_Template, which is in its turn inherited from Mage_Core_Block_Abstract.
In Magento 2 it is inherited from Magento\Framework\View\Element\Template, which is also inherited from Magento\Framework\View\Element\AbstractBlock.

In which way does a template block store information about its template file?

In Magento 1 all templates are described with regard to the theme. For example, if you use the theme from the package base with the name default, so when using the template checkout/onepage/link.phtml, the file from app/design/frontend/base/default/checkout/onepage/link.phtml will be used.
In Magento 2 module scope has been added, but it’s non-mandatory. As an example, let’s compare layout code for Magento 1:   

Here is in Magento 2:

In this case, the template onepage/link.phtml will use the template vendor/magento/module-checkout/view/frontend/templates/onepage/link.phtml (if the template hasn’t changed) or app/design/frontend/base/default/Magento_Checkout/templates/onepage/link.phtml (if the template has changed or you use the base/default theme).
Moreover, in Magento 2 you needn’t specify module scope, if the template layout is within the module (in this context Magento_Checkout).

In Magento 1 there’s a possibility to specify the template by using the method setTemplate in the class Mage_Core_Block_Template.
In Magento 2 you can also use the method setTemplate in the class Magento\Framework\View\Element\Template.

Does it store an absolute or a relative path to the template?

In Magento 2 as well as in Magento 1 a relative path to the template is used.

What is the role of the Mage_Core_Block_Abstract class?

The given class (in Magento 2 Magento\Framework\View\Element\AbstractBlock) is the parent of all blocks and implements basic functional for them (for example, child elements addition and their removal, parent block data, data structure storage for a block).



Post a new comment

top
BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *