Send Data from Controller to PHTML – Magento 1 Guide

controllersdesignmagento-1phtml

I have this controller:

...
public function insertAction() {
    $something = 'testcase';
    $this->loadLayout();
    $this->_title($this->__("the title"));
    $this->renderLayout();
}
...

I would like to access $something in my .phtml document, how do I do this? (or to put it in another way: how can I access $something in my .phtml file)

Best Answer

There are a couple of ways to do this.

Assign directly to the Block:

$block->assign($var);

or

$block->assign(array('myvar'=>'value','anothervar'=>true));

Then you can access it in the phtml file like this:

$this->myvar

Use the Mage registry:

Mage::register('custom_var', $var);

and then use it like:

$var = Mage::registry('custom_var');