Friday, March 1, 2013

Different ways of accessing a Zend View Helper

A Zend View Helper is simply a class, which follows a certain convention.When attached to a view object, you can call the helper as if it were a method of the view object itself. The View object retains helper instances,which means that they retain states between calls.
By convention we should use these view helpers in the View Layer only, but there are some circumstances we end up breaking this convention, start using these helpers in Controller, Helpers and Models. Lets see, how we can access the View Helpers from the differnt parts
//In Action Controller, you can access the view helper like below
$this->view->helpername();
or
$this->view->getHelper('helpername');

//In Action Helper, you can access the view helper like below
$view = $this->getActionController()->view;
$view->helpername();

//Suppose if you are in the Model, where you don't have any 
//reference to Zend, this is how you have to access
$viewObject = Zend_Controller_Action_HelperBroker::getStaticHelper('ViewRenderer')->view;
$viewObject->helpername();


Happy Programming ...!!!

2 comments: