Zend\View (finding view scripts)

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Zend\View (finding view scripts)

gargoyle-3
I've been having a play round with the Skeleton app and some modules, and looking for ways to improve how module view scripts are discovered and used. I'll first explain what I was playing around with and the direction I was heading, then some other suggestions at the bottom.

The tl;dr version is that view->render() is going to need more information than just a script name, or the resolver process needs moving out of view->render() to resolve the script name first.



I started by altering the View\Listener.php's renderView() method to get the controller name from $e->getTarget() because the controller name you get from getRouteMatch() is the alias in the config, and not the full name-spaced class name.

I then updated the call to view->render($script) to be view->render(array('namespace' => 'script')) Along with the following update config section:-

        'Zend\View\PhpRenderer' => array(
                'parameters' => array(
                    'resolver' => 'Application\View\TemplateMap',
                    'options'  => array(
                        'script_map' => array(
                            'Application\Controller\IndexController' => __DIR__ . '/../views/index',
                            'Application\Controller\ErrorController' => __DIR__ . '/../views/error',
                            'Application\Controller\BlogController' => __DIR__ . '/../views/blog',
                        ),
                        'layout_map' => array(
                            'Layout' => __DIR__ . '/../views/layouts'
                        ),
                    ),
                ),
            ),


The Application\View\TemplateMap class simply implemented the \Zend\View\TemplateResolver interface with a modified getScriptPath() to look for an action script based of the 'script_map' array.

Apart from the ugly layout_map hack, it sort of worked… Until the IRC meeting and __DIR__ is going to be removed from config files. And after skimming over the IRC log, and the other thread, it seems I came up with a similar idea to others in that perhaps this should be handled inside Module.php?

I kind of like the way I was going with this, in that it tries to do the same with view scripts as the auto-loader does for classes? But I think it's going to need changes to Zend\View and I am hitting the limit of my ZF(2) knowledge when it comes to the View part of the MVC.


Paul
(Gargoyle)


Reply | Threaded
Open this post in threaded view
|

Re: Zend\View (finding view scripts)

weierophinney
Administrator
-- Paul Court <[hidden email]> wrote
(on Thursday, 08 December 2011, 02:25 PM +0000):
> I've been having a play round with the Skeleton app and some modules,
> and looking for ways to improve how module view scripts are discovered
> and used. I'll first explain what I was playing around with and the
> direction I was heading, then some other suggestions at the bottom.
>
> The tl;dr version is that view->render() is going to need more
> information than just a script name, or the resolver process needs
> moving out of view->render() to resolve the script name first.

First, and honestly -- this is a HUGE step forward, even if you have
lingering questions.

The route you took regarding the resolver is correct -- the idea behind
the Resolver interface is to take a script "name" and resolve it to a
file name. The only thing I see in what you did that I'd do differently
is that the "script_map" and "layout_map" should be pushed to the
resolver implementation you use, and then simply inject the resolver
into the renderer.

As for _where_ this happens, especially with regards to view scripts
relative to individual module directories -- that's still an open
question.

Thanks for the start -- this is great!

> I started by altering the View\Listener.php's renderView() method to
> get the controller name from $e->getTarget() because the controller
> name you get from getRouteMatch() is the alias in the config, and not
> the full name-spaced class name.
>
> I then updated the call to view->render($script) to be
> view->render(array('namespace' => 'script')) Along with the following
> update config section:-
>
> 'Zend\View\PhpRenderer' => array(
>                 'parameters' => array(
>                     'resolver' => 'Application\View\TemplateMap',
>                     'options'  => array(
>                         'script_map' => array(
>                             'Application\Controller\IndexController' => __DIR__ . '/../views/index',
>                             'Application\Controller\ErrorController' => __DIR__ . '/../views/error',
>                             'Application\Controller\BlogController' => __DIR__ . '/../views/blog',
>                         ),
>                         'layout_map' => array(
>                             'Layout' => __DIR__ . '/../views/layouts'
>                         ),
>                     ),
>                 ),
>             ),
>
>
> The Application\View\TemplateMap class simply implemented the
> \Zend\View\TemplateResolver interface with a modified getScriptPath()
> to look for an action script based of the 'script_map' array.
>
> Apart from the ugly layout_map hack, it sort of worked… Until the IRC
> meeting and __DIR__ is going to be removed from config files. And
> after skimming over the IRC log, and the other thread, it seems I came
> up with a similar idea to others in that perhaps this should be
> handled inside Module.php?
>
> I kind of like the way I was going with this, in that it tries to do
> the same with view scripts as the auto-loader does for classes? But I
> think it's going to need changes to Zend\View and I am hitting the
> limit of my ZF(2) knowledge when it comes to the View part of the MVC.

--
Matthew Weier O'Phinney
Project Lead            | [hidden email]
Zend Framework          | http://framework.zend.com/
PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc

--
List: [hidden email]
Info: http://framework.zend.com/archives
Unsubscribe: [hidden email]