Passing values and parameters to template file using XML and ShortCode from Magento Admin

Passing values and parameters to template file using XML and ShortCode from Magento Admin

At times there is a need to pass some parameters to template file in Magento. Some of the conditions are as follow:

1) Category Id is needed to be passed to display all the products of that particular category
2) Static block id needs to be passed to call it in template file.

We can pass any parameters in the template file from Magento Admin using two methods

1) Using the shortcode: By using shortcode we can pass the parameter as follows

{{block type="greeting_products/productslider" category_id="11" product_count="10" title="Featured Products" template="greeting/products/featuredslider.phtml" }}

Here category_id and product_count are passed in the file featuredslider.phtml

{{block type="core/template" name="contactForm"  form_action="contacts/index/post"  template="contacts/form.phtml"}}

Here form_action URL is passed as a form action in contact form which will be passed to postAction method of IndexController.php file.

2) Using XML: We can also pass parameters using XML. It is bit tricky but is achievable. Basically we need to use a predefined tag named <action> to pass the value in template file. This can be done in following way

<block type="greeting_products/productslider" name="featuredProduct" template="greeting/products/featuredslider.phtml">
<action method="setCategoryId"><category_id>11</category_id></action>
<action method="setProductCount"><product_count>10</product_count></action>
<action method="setTitle"><title>Featured Products</title></action>

Here setCategoryId, setProductCount and setTitle are use to passed the value to the template file.

<block type="core/template"  name="contactForm" template="contacts/form.phtml">
<action method="setFormAction"><url>contacts/index/post</url></action>

Here setFormAction is used to set the Action URL of the form.

These values can be retrieved using following code from the template file, block file and controller file of the module using below code:


Please share your feedback on the above post.

Leave a Comment