Categories


Archives


Recent Posts


Categories


Magento 2 Layout Changes

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

Updated for Magento 2! No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

I’ve been digging into Magento 2’s layout system, and there’s significant changes from Magento 1. Conceptually, it’s still a “nested tree” of elements, but instead of just blocks there’s now a second type of thing you can add to a layout called a container. Containers look like they’re intended to formalize the difference between content and structural blocks.

Simple enough from an end user’s point of view — but there’s a number of changes under the hood in the Mage_Core_Model_Layout element to implement this. From an initial pass, these changes include

  1. The layout’s parent/child structure is now managed by a new Mage_Data_Structure object (stored in the _structure object property)

  2. Blocks are still block objects, but containers are not independent objects

  3. Containers are rendered by the layout object, whereas blocks are still rendered by the toHtml method (see renderElement and _renderContainer)

This means if you have code like this

$layout->getBlock('content');

you’re in for some refactoring, as content is no longer a block, but instead it’s a container.

Beyond my interest in porting the world’s greatest Magento debugging extension to Magento 2, this is also interesting as PHP architecture case study.

If Magento was a java based system, my bet is containers would have simply become another type of Block object. However, because each new instantiated PHP object brings an additional bit of overhead, and Magento is the rare PHP application where the PHP code actually becomes a bottle neck, it’s easy to see why the container implementation takes a different approach.

Hat tip to @vrann for the containers pointer.

Copyright © Alana Storm 1975 – 2023 All Rights Reserved

Originally Posted: 22nd September 2013

email hidden; JavaScript is required