Search code examples
phpzend-frameworkzend-view

Where do I put view scripts needed by view helpers (using Zend_View and the default directory layout)?


I've got a relatively complicated portion of my application, which is an editor for access control lists. I need to reuse it in a few places, and I'd like it to be loadable all ajax-y and such.

Because I need to use it often, I'd like to make it into a Zend_View_Helper. That's simple -- just put $view->setHelperPath(APPLICATION_PATH . '/views/helpers', 'Cas_View_Helper'); in the bootstrap for the view, and all seems to be set with regards to loading the view helper.

However, the helper really should be output using a view script. Is there a standard location where I should put that?


Solution

  • Usually, when a helper need a view script this script is placed inside a partial. The location of that partial may vary depending on your directory structure, but the standard is:

    application[/modules/name]/views/scripts/partials/
    

    You can write a helper with something like this:

    class Cas_View_Helper_Foo extends Zend_View_Helper_Abstract 
    {
        protected $partial = 'partials/foo.phtml';
        protected $view = null;
    
        // Render the partial here
        public function foo($params)
        {
            $this->view->partial($this->partial, $params);
        }
    
        // Specifying this method Zend_View will inject the view here
        public function setView(Zend_View_Interface $view)
        {
            $this->view = $view;
        }
    
    }