How to set data in Magento Layout Update XML to make it available for use in templates

If you need to set data in a block so that you can use it in the block view file then you can do it in the layout xml files or in Magento admin using the Layout Update XML/Custom Layout Update XML field.

Suppose you want to differentiate a category based on a variable “category_type”, if the value of “category_type” is “sale” then you will display category image else not on the category landing page.

Navigate to Admin >> Catalog >> Categories >> Manage Categories. Click on the category that you want to mark as a “sale” category to edit it. We assume that “Is Anchor” is set as “Yes” for the category in the “Display Settings” tab.

Now open the “Custom Design” tab and paste the following xml in the “Custom Layout Update” textbox and save the category.


<reference name="category.products">
     <action method="setData">
         <name>category_type</name>
         <value>sale</value>
     </action>
</reference>

Instead of the above code the following can also be used:


<reference name="category.products">
     <action method="setCategoryType">
         <value>sale</value>
     </action>
</reference>

Now in the category view file catalog\category\view.phtml, you get the value of the variable as shown below:


$categoryType = $this->getCategoryType();

If you need to set a variable that should be available in all anchored categories then you can add the following code in the theme’s local.xml file:


<catalog_category_layered translate="label">
     <reference name="category.products">
         <action method="setCategoryType">
             <value>sale</value>
         </action>
     </reference>
</catalog_category_layered>

 

Points to be noted:

 

  1. In the below XML:
    <reference name="category.products">
         <action method="setData">
             <name>category_type</name>
             <value>sale</value>
         </action>
    </reference>
    
    

    The tags “<name>” and “<value>” have no meaning, they can be anything containing a value as shown below:

    
    <reference name="category.products">
         <action method="setData">
             <a>category_type</a>
             <b>sale</b>
         </action>
    </reference>
    
    
  2. If you enter multiple tags as shown below the only the first two tags are considered:
    <reference name="category.products">
         <action method="setData">
             <name>category_type</name>
             <value>sale</value>
             <name2>page_type</name2>
             <value2>new</value2>
         </action>
    </reference>
    
    

    Here only the “<name>” and the “<value>” tags are considered while “<name2>” and “<value2>” get ignored.

  3. Now lets check the second way of setting variable that we mentioned above:
    <reference name="category.products">
         <action method="setCategoryType">
             <value>sale</value>
         </action>
    </reference>

    Here we are setting data like the setter in Magento. Again “<value>” has no meaning, it can be any tag containing a value as shown below:

    
    <reference name="category.products">
         <action method="setCategoryType">
             <xyz>sale</xyz>
         </action>
    </reference>
    
    

Leave a Comment

Back to top