Magento: Passing a value from a controller into the phtml

Another Magento quickie for when you want to pass a value into your phtml template file that is set/generated inside the controller.

Inside the controller you can do this:

$block->assign(array(“myVarName”=>”value”));

Where $block could be something like

$block = $this->getLayout()->getBlock(“head”) ;

if you wanted to pass the value into the head template.

You can also set a value like this:

$block->setData(“myVarName”,”value”)

Then, inside the phtml template file you can get the value like this

$this->myVarName

Note you can pass multiple variables through a single $block->assign() like this

$block->assign(array(
“myVarName”=>”value”,
“anotherVar”=>123,
“enoughVars”=>true
));

Facebook Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

Apply your human brain cells and complete this highly complicated maths problem *