Recent Posts


Magento 2 Layout: Multiple XML Trees


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!

One interesting thing about Magento 2’s refactored layout system is how it enables multiple XML trees modifying one global structure object. The layout builder for page objects

#File: vendor/magento/framework/View/Page/Builder.php
protected function generateLayoutBlocks()
    return parent::generateLayoutBlocks();

 * Read page layout and write structure to ReadContext
 * @return void
protected function readPageLayout()
    $pageLayout = $this->getPageLayout();
    if ($pageLayout) {
        $readerContext = $this->layout->getReaderContext();
        $this->pageLayoutReader->read($readerContext, $pageLayout);

defines a generateLayoutBlocks that will load a separate XML tree (the files in page_layout folders, I belive), and pass that XML tree through the the reader/schedule objects. It then calls the parent generateLayoutBlocks method, which handles loading, merging, read scheduling, and generating blocks from the files in the layout folders.

Two separate XML trees, both modifying the same layout.

Copyright © Alan Storm 1975 – 2022 All Rights Reserved

Originally Posted: 20th February 2016