Skip to main content

I decided to learn and understand the way magento reads and generates layout xml files. I used some articles and the debbuging of code. I wrote some moments, which I found interesting and important for understanding magento working with layouts during my debbuging. As an example I used checkout/cart page and I had already installed frontend theme smartwave/porto.

1. Call function in the CartController.php $this->loadLayout()
2. This function is the method of abstract class Mage_Core_Controller_Varien_Action
3. It has 3 parameters: $handles = null, $generateBlocks = true, $generateXml = true

Here is a good explanations of arguments.

$handles: This argument is either a string or array of strings. Each string is a name of layout handle. If this argument is passed, the specified handle(s) are added to the Layout Update Instance. If the argument is not passed or passed as null, the handle default is added automatically. If the argument is passed as a blank string or false, then the default handle is not added.

$generateBlocks: This is a Boolean argument with default value of true. If it is set to false, the blocks defined in the layout XML are not instantiated.

$generateXml: This is also a Boolean argument with default value of true. If it is set to false, Layout updates are loaded but not applied to the page. Also the argument $generateBlocks would have no effect in this case and no layout blocks are instantiated.”

“If the argument is passed as a blank string or false, then the default handle is not added.” In my opinion, it is interesting information about parameter $handles

4. addActionLayoutHandles()  the method of abstract class Mage_Core_Controller_Varien_Action. It takes a package (name – it is name of the package) and adds handles from that package
'THEME_'.$package->getArea().'_'.$package->getPackageName().'_'.$package->getTheme('layout')  in my case it was 'THEME_frontend_smartwave_porto'

and full name of action also is added  in my case it was checkout_cart_index

5. loadLayoutUpdates() (abstract class Mage_Core_Controller_Varien_Action)
(There is an event – controller_action_layout_load_before)
There is a method where all updates (if i understand it in a right way, updates is everything placed in xml files in tags ) are loaded, merged and saved in cache (for that step it is just their names).

6. Next is the method generateLayoutXml() (abstract class Mage_Core_Controller_Varien_Action)
(There is an event controller_action_layout_generate_xml_before)
There is no any blocks in the layout yet.
And here is a place where common xml is generating from different xml files were taken before.

You can follow to function asSimplexml (class Mage_Core_Model_Layout_Update and see everything was generated).

Magento reads the generated xml.
The first it takes information from tags and ignores blocks and references were marked.
Magento sets the xml and wrote all information in the _node.

7. Method generateLayoutBlocks() (abstract class Mage_Core_Controller_Varien_Action)
(There is an event controller_action_layout_generate_blocks_before)
Magento takes the _node was setted before, check ifconfig and adds blocks, references and actions and add blocks to layout for output.

I stopped debbuging before layout rendering.

Also I found useful article which describes how to use layout xml files, how to write information in it in a right way.

Vladimir Repalo

Vladimir Repalo

Magento Developer at Mobecls, 8+ years of experience. Feel free to ask me anything about this post in the comments below.