简体   繁体   中英

How to pass variable from block to phtml in magento

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.

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();

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

Or in your controller, after

 $this->loadLayout();

but before

 $this->renderLayout();

use this code:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM