Through the last posts you can learn about Magento layout XML schema, the way to register layout XML files, create and add the code to pages. In order to keep sharing you knowledge of Magento layout, today I will show you how to pass variables from a layout to a block.
1. Pass variables from a layout to a block
• It’s possible to insert variables into a block from a .xml file by using:
<action method="setData"><name>category_id</name><value>3</value></action>
    • Use the action tag with the method=”setData” attribute (setData method of block is called when the system loads layout)
    • Children tags are name, value (they are also parameters of setData method)
    • In the block file, this value is taken by:
$categoryId = $this->getCategoryId();
$categoryId = $this->getData(‘category_id’);

• Besides, we can put variables into a block by {{block}} structure (used in CMS page)

{{block type="catalog/product_list" manufacturer_id=14
template="catalog/product/brand.phtml"}}

2. Add and customize Javascript

• Use the action tag with the method=”addJs” attribute
For example:

<reference name="head">
<action method="addJs"><script>test/test.js</script></action>
</block></code>

• Or you can use action tag with the method=”addItem” attribute if your javascript file is in the skin folder.

For example:

<reference name="head">
<action method="addItem"><type>skin_js</type><name>js/test.js</name><params/></action>
</block>

This tutorial is the final part of Magento layout and also ends the Rendering topic. Hope our Magento articles will bring you valuable contributions. Good day and have fun! 😉

Author

Why Magestore? We believe in building a meaningful & long-term relationship with you.

Write A Comment