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

4

5

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?

Atabrine answered 8/10, 2010 at 1:54 Comment(0)
I
8

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;
    }

}
Inigo answered 19/10, 2010 at 16:22 Comment(0)
G
1

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.

Gunshot answered 19/10, 2010 at 4:37 Comment(1)
Yes, I know I could just put them somewhere, but I want to know if there's a "standard" place to put them.Atabrine
R
1

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.

Royden answered 22/10, 2010 at 19:47 Comment(0)
B
0

In the view folder you make a folder helper. Here you put the view helpers.

Brassy answered 19/10, 2010 at 16:4 Comment(1)
Yes, I've done that. But the helper needs a view script itselfAtabrine

© 2022 - 2024 — McMap. All rights reserved.