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?
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;
}
}
You could always add a "shared" view path using
$view->addScriptPath('/path/to/shared/view/scripts')
http://framework.zend.com/manual/en/zend.view.controllers.html#zend.view.controllers.script-paths.
Also, have a look at how Zend_Paginator handles partial view rendering.
There is no one standard, recommendation or anything set in stone about how to do this. ZF being as flexible as it is, leaves that really up to you for whatever is best for your application. I'll use this answer for a comparison of the current solutions.
Keyne answer is the solution that I feel most people eventually come to. Personally I have my own twist: application[/modules/name]/views/scripts/helpers/{your helper}/blah.phtml
Which allows you to not have conflicting partial names, and be able to easily navigate to your view helpers file.
Although this still sucks balls for modularity, view helpers have to be installed in at least two places. Alternatively another solution which is quite popular is to do:
application[/modules/name]/views/helpers/{your helper}.php
application[/modules/name]/views/helpers/{your helper}/{view scripts}.phtml
Which provides a bit better modularity for your view helpers.
In the view folder you make a folder helper. Here you put the view helpers.
精彩评论