Magento – Magento 1.9: How to pass a variable from controller to setTemplate block

magento-1.9

I have to pass a variable to a block .phtml file. I am setting layout in the controller by setTemplate() function. Do you have any idea?

Best Answer

Here I have a solution might work for you. We mostly use setData() method for pass variables from the controller to block.

$variableValue = 10000;
$block = $this->getLayout()->getBlock('blockNameHere');
$block->setData('variable', '$variableValue')->setTemplate('pathto/block.phtml');

above setData() method will create a variable, and you can access this variable in the block file block.phtml you created. Use below code for getting the value of the variable.

echo $this->variable;
Related Topic