Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass variable from block to phtml in magento

Tags:

php

magento

block

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.

like image 794
Mahmood Rehman Avatar asked Nov 02 '12 11:11

Mahmood Rehman


3 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();
like image 174
Bas Tuijnman Avatar answered Sep 20 '22 21:09

Bas Tuijnman


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

like image 27
LittleBigDev Avatar answered Sep 23 '22 21:09

LittleBigDev


Or in your controller, after

 $this->loadLayout();

but before

 $this->renderLayout();

use this code:

$this->getLayout()->getBlock('multibanners')->setCategoryId(42);
like image 31
Raif Atef Avatar answered Sep 23 '22 21:09

Raif Atef