Object-specific Layout Update Handles

Object-specific Layout Update Handles

Every HTTP request in Magento results with a few layout handles which can be used to customize layout of desired pages. If you ever tried to dump layout handles in Magento, you would see many of them. These handles are calculated differently, based on different variables.

Depending on whether the user is logged in or not, Magento uses customer_logged_in and customer_logged_out handles. Different stores have also different layout updates (e.g. STORE_default, STORE_cars, STORE_fashion…).
Themes have their own layout handles, e.g. THEME_frontend_default_default, THEME_frontend_enterprise_default.

While we were working on a website that uses “Shop by Brand” extension, client asked us if we could change layout for one brand page. In order to provide that functionality we had to extend this extension in a way that every brand page has it’s own layout update handle.

You might noticed that Magento uses this functionality to create unique layout handles for different categories (e.g. CATEGORY_96, CATEGORY_35) and products (e.g. PRODUCT_22735, PRODUCT_225).

Here I’ll show you how you can create object-specific layout handle functionality for your custom modules. Handle will be composed of string concatenated to object’s id (like OUR_COOL_OBJECT_535, OUR_COOL_OBJECT_863).

1. Create Observer

In order to add handle to Magento’s layout update object before it’s too late, we have to observe controller_action_layout_load_before event.


2. Add Handle

Now we have to calculate layout update handle and add it to the layout update object.

class Inchoo_LayoutHandle_Model_Observer
public function controllerActionLayoutLoadBefore(Varien_Event_Observer $observer)
/** @var $layout Mage_Core_Model_Layout */
$layout = $observer->getEvent()->getLayout();
$id = Mage::app()->getRequest()->getParam('id');
/* or */
if($ourCoolObject = Mage::registry('our_cool_object'))
$id = $ourCoolObject->getId();

That’s it!
Hope this helps! 🙂

You made it all the way down here so you must have enjoyed this post! You may also like:

Magento 2 Custom CMS page layout Matej Djakovic
Matej Djakovic, | 7

Magento 2 Custom CMS page layout

Override Magento 2 Layout: Product page example Mladen Ristic
, | 6

Override Magento 2 Layout: Product page example

Bypassing Magento 2 layout update restrictions in Admin Adrian Bece
Adrian Bece, | 2

Bypassing Magento 2 layout update restrictions in Admin


  1. will Mage::registry(‘our_cool_object’) return null? can’t see any registration? don’t get it 🙁

  2. We’ve been using some additional handles for most of our products. We’ve created a module H&O Handles which adds some interesting handles.

    If you are viewing a category: /products/dvds.html (category id: 5)

    By default it adds the handle CATEGORY_5

    With our module we add the following:

    This allows us finer granularity for adding designs to pages.

    We also add these handles to products and CMS-pages, but the following prefix:

    Also attribute set handles:

    For more info, view: https://github.com/ho-nl/Ho_Handles

    1. link is broken Paul. Can you please fix this? I really need to know how to implement something like CATEGORY_2_child_child. In my case PRODUCT_TYPE_simple_ATTRIBUTE_SET_ID_4 something like this

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <blockquote cite=""> <code> <del datetime=""> <em> <s> <strike> <strong>. You may use following syntax for source code: <pre><code>$current = "Inchoo";</code></pre>.

Tell us about your project

Drop us a line. We'd love to know more about your project.