Home » Php » How to pass variable from block to phtml in magento

How to pass variable from block to phtml in magento

Posted by: admin July 12, 2020 Leave a comment

Questions:

I want to pass value from block but I am unable to get the desire results.
Something going wrong which I don’t know why.
I try the code "block type="multibanners/multibanners" name="multibanners" category_id="5" alias="multibanners" in xml file.
To get results I used echo $this->getCategoryId() and getData('category_id').
I search but all time the same code.
Can anyone help me whats wrong with the code.

Thanks.

How to&Answers:

You can set your variables like this:

<block type="multibanners/multibanners" name="multibanners" alias="multibanners">
<action method="setData"><name>category_id</name><value>5</value></action>
</block>

and retreive it like this:

$this->getCategoryId();

Answer:

Mage_Core_Block_Template::assign() is made for that

Example of use in Mage_Page_Block_Html_Breadcrumbs::_toHtml() + direct call of $crumbs var in the breadcrumbs.phtml template file

Answer:

Or in your controller, after

 $this->loadLayout();

but before

 $this->renderLayout();

use this code:

$this->getLayout()->getBlock('multibanners')->setCategoryId(42);