Magento – Pass variables to block

With this post I will described various ways to pass variables to block.

1. Run time in PHTML file

$productId = 10;
$this->getChild('testblock')->setData("productId", $productId);
echo $this->getChildHtml('testpage', fales);

NOTE: second parameter ‘false’ is to tell, not to cache block.

2. Pass variables from a layout to a block
Yes, We can insert variable into a block from layout xml file as bellow,

<reference name="root">
   <block type="yourmodule/testblock" name="testpage">
        <action method="setData"><name>product_id</name><value>10</value></action>

Using the action tag with the method=”setData” attribute. Its child tags name and value to set variable and its value.
setData method of block is called when the system loads layout. name, value tags are parameters of setData method.

In block file you can access this by,

$productId = $this->getProductId(); 
$productId = $this->getData(‘product_id’);

3. Used in CMS page
we can put variables into a block by,

{{block type="yourmodule/testblock" product_id=10 template="module/yourmodule/bestproduct.phtml"}}

7 thoughts on “Magento – Pass variables to block

  1. Pingback: Magento – Pass variables to block | Kenneth Carnesi

  2. sorry, my skills aren’t up to scratch. how do I use the above to pass a value from CMS to phtml?

    How do I pass my_value…

    {{block type=”yourmodule/testblock” my_value=ABC template=”module/yourmodule/bestproduct.phtml”}}

    to a phtml file so I can “echo my_value”??

  3. Actually, let me be clear…

    In my CMS, I have…

    {{block class=”Itoris\Producttabsslider\Block\Frontend\Description” template=”Itoris_Producttabsslider::product/view/specifications.phtml}}

    and in my phtml I have…

    echo $_product->getResource()->getAttribute(‘specifications’)->getFrontend()->getValue($_product);

    This setup currently displays the contents of my “specifications” attribute on my product page, but it means I have to create a separate phtml for each attribute I want to display.

    I’m hoping I can create a generic “displayattribute.phtml” which pulls in a variable from CMS where I specify which attribute I want to display, the phtml then parses the value into my echo line and spits back the contents of the attribute i specified back to the product view page.

  4. Me again, I worked it out, thanks to your page and this page

    On my CMS page I enter…

    {{block class=”Itoris\Producttabsslider\Block\Frontend\Description” display_Attribute=”specifications” template=”Itoris_Producttabsslider::product/view/displayattribute.phtml}}

    and in the phtml file I enter…

    echo $_product->getResource()->getAttribute($block->getData(‘display_Attribute’))->getFrontend()->getValue($_product);

    Hope that helps others…

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s