Magento Themes and Magento Extensions

Pass data from controller to view in magento

Share

It’s may be very easy for Zend framework user But, I find some magento developer fighting to pass data from controller to view.
In magento you have to create a empty model(if your mod don’t have a varient object model) which extends to varient object. Magento use magic getter magic setter so you can pass value by setAnyname() and getAnyname() thus functions will be auto defines by Zend like many other framework do.

Model file:

1
2
3
Packname_Modulename_Model_Viewinfo extends Varien_Object{
}

Controller function:

1
2
3
4
5
6
7
public function indexAction()
{
$bla = Mage::getSingleton('Modulename/viewinfo');
$bla->setMsg("Thank You for your visit!");
$this->loadLayout();
$this->renderLayout();
}

Now in your View .phtml file add:

1
2
<?php $blas = Mage::getSingleton('Modulename/viewinfo'); ?>
<?php echo $blas->getMsg(); ?>

Comments are closed.