Categories


Archives


Recent Posts


Categories


Automatically Assigning a Parent Block via. Layout XML

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.

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

So here’s something I didn’t know about until 15 minutes ago. When you’re creating a Layout Update XML block, you can automatically assign a block you’ve created as a child of another block with a parent attribute in your block tag. That means this

<block type="foo/bar" parent="block_name_in_layout" ... />

is sort of like the following (oversimplified) PHP code

$block = $layout->createBlock('foo/bar');
$parent = $layout->getBlock('block_name_in_layout');
$parent->insert($block)

Or the more verbose Layout XML code

<reference name="block_name_in_layout">
    <block type="foo/bar" ... />
</reference>

Props to a benmarks tweet I ran across that made me think he was drunk until I peeked at app/code/core/Mage/Core/Model/Layout.php

Copyright © Alan Storm 1975 – 2017 All Rights Reserved

Originally Posted: 8th December 2011