Developing software in the Real World

View Helper Case Notes

This is how it works:

views/helpers/MyHelper.php contains class Zend_View_Helper_MyHelper which has a function called myHelper()

Also, if you add a function called setView() to your class, then the View will pass you an instance of itself before it calls your view helper function.

i.e.

Class Zend_View_Helper_MyHelper
{
protected $_view;

public function setView($view)
{
$this->_view = $view;
}

public function myHelper($myVar)
{
return $this->_view->escape($myVar);
}

}

5 thoughts on “View Helper Case Notes

  1. Hi Rob,
    I have a small question about this convention: Why we have to prefix the helper classes with "Zend_View_Helper_"? it is, from my point of view, unnessacery. Moreover if you want use this convention let's create a base class Zend_View_Helper_BaseHelper which includes setView($view) and everyone else will inherit from that class.

    Hope it is not too late,
    Thank you for your time,
    Roy

  2. Roy,

    The reason that Zend_View_Helper is there is so that the default view helpers map to the directory where the view helper is stored.

    You can set the prefix to whatever you like when you set up the helper path for your own helpers:


    $view->setHelperPath('/path/to/more/helpers', 'My_View_Helper');

    Regards,

    Rob…

  3. Hi Rob,

    Do you have experience with Zend_View's filter? I seek high and low but couldn't get any useful info on this topic.

    Thanks,
    Loke

Thoughts? Leave a reply

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