|
I'm finding it difficult to see how the view manager's template map works.
If I look at the Zend Skeleton project, the Application module has the following for its template map: 'template_map' => array( 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', 'index/index' => __DIR__ . '/../view/index/index.phtml', 'error/404' => __DIR__ . '/../view/error/404.phtml', 'error/index' => __DIR__ . '/../view/error/index.phtml', ), When I look at Matthew Weier O'Phinney's blog app source, his Application module has the following for its template map: 'template_map' => array( 'about' => __DIR__ . '/../view/about.phtml', 'analytics' => __DIR__ . '/../view/analytics.phtml', 'error' => __DIR__ . '/../view/error.phtml', 'layout' => __DIR__ . '/../view/layout.phtml', 'projects' => __DIR__ . '/../view/projects.phtml', 'searchbox' => __DIR__ . '/../view/searchbox.phtml', 'search' => __DIR__ . '/../view/search.phtml', 'pages/404' => __DIR__ . '/../view/pages/404.phtml', 'pages/comics' => __DIR__ . '/../view/pages/comics.phtml', 'pages/home' => __DIR__ . '/../view/pages/home.phtml', 'pages/resume' => __DIR__ . '/../view/pages/resume.phtml', ), However, when I tried to write something like the following, I got errors: 'template_map' => array( 'layout' => __DIR__ . '/../view/layout/default.phtml', 'index/index' => __DIR__ . '/../view/index/index.phtml', 'about/index' => __DIR__ . '/../view/about/index.phtml', ... ), When I would go to the about page (mydomain.com/about), ZF would tell me that it couldn't find the application/about/index view script. So, I would have to change it to the following in order to get it to work: 'template_map' => array( 'layout' => __DIR__ . '/../view/layout/default.phtml', 'application/index/index' => __DIR__ . '/../view/index/index.phtml', 'application/about/index' => __DIR__ . '/../view/about/index.phtml', ... ), Can anyone tell me why this is the case? Additionally, it would be nice to know what tells the view manager which script(s) to use. If there is already documentation on all of this, please point me in the right direction. I've looked, but the docs are pretty sparse thus far, I think. Thanks in advance... |
|
Administrator
|
-- travis <[hidden email]> wrote
(on Wednesday, 30 May 2012, 04:29 PM -0700): > I'm finding it difficult to see how the view manager's template map works. > > If I look at the Zend Skeleton project, the Application module has the > following for its template map: > 'template_map' => array( > 'layout/layout' => __DIR__ . '/../view/layout/layout.phtml', > 'index/index' => __DIR__ . '/../view/index/index.phtml', Technically, this one above should be "application/index/index". More on that below. > 'error/404' => __DIR__ . '/../view/error/404.phtml', > 'error/index' => __DIR__ . '/../view/error/index.phtml', > > ), > > When I look at Matthew Weier O'Phinney's blog app source, his Application > module has the following for its template map: > 'template_map' => array( > 'about' => __DIR__ . '/../view/about.phtml', > 'analytics' => __DIR__ . '/../view/analytics.phtml', > 'error' => __DIR__ . '/../view/error.phtml', > 'layout' => __DIR__ . '/../view/layout.phtml', > 'projects' => __DIR__ . '/../view/projects.phtml', > 'searchbox' => __DIR__ . '/../view/searchbox.phtml', > 'search' => __DIR__ . '/../view/search.phtml', > 'pages/404' => __DIR__ . '/../view/pages/404.phtml', > 'pages/comics' => __DIR__ . '/../view/pages/comics.phtml', > 'pages/home' => __DIR__ . '/../view/pages/home.phtml', > 'pages/resume' => __DIR__ . '/../view/pages/resume.phtml', > ), My case is different. I've created a custom controller that does not extend from ActionController, and which sets the specific template it intends to invoke directly on the ViewModel returned. (Those are the "pages/*" templates.) The top-level templates are either error, layout, or partial templates. More below... > However, when I tried to write something like the following, I got errors: > 'template_map' => array( > 'layout' => __DIR__ . > '/../view/layout/default.phtml', > 'index/index' => __DIR__ . > '/../view/index/index.phtml', > 'about/index' => __DIR__ . > '/../view/about/index.phtml', > ... > ), > > When I would go to the about page (mydomain.com/about), ZF would tell me > that it couldn't find the application/about/index view script. So, I would > have to change it to the following in order to get it to work: > 'template_map' => array( > 'layout' => __DIR__ . > '/../view/layout/default.phtml', > 'application/index/index' => __DIR__ . > '/../view/index/index.phtml', > 'application/about/index' => __DIR__ . > '/../view/about/index.phtml', > ... > ), > > Can anyone tell me why this is the case? Additionally, it would be nice to > know what tells the view manager which script(s) to use. If there is > already documentation on all of this, please point me in the right > direction. I've looked, but the docs are pretty sparse thus far, I think. As of beta4, the InjectTemplateListener now injects a template based on the controller triggering the event. The rules it follows are: * It normalizes the top-level (module) namespace of the controller. In the case of the Application module, that becomes "application". In the case of a module named "FooBar", it becomes "foo-bar". * It normalizes the controller name. If it ends in "Controller", that segment is stripped off. "FooController" becomes "foo"; "FooBarController" becomes "foo-bar". * It normalizes the action route parameter, if any. "foo" becomes "foo"; "fooBar" becomes "foo-bar". The segments are then concatenated using "/" as the separator: <module namespace>/<controller name>[/<action>] Now, at any time, you can set the template name _directly_ on the view model; if you do this, the InjectTemplateListener simply returns immediately. This is the route I took in my application; however, in my _modules_, I usually follow the default mechanism, as it's predictable, and maps to the filesystem easily. (The above is documented in the MVC docs, BTW.) -- 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] |
I have a number of modules namespaced in my modules folder (e.g. modules\MyNamespace\MyModule) and this functionality makes all their templates try and load from that name rather than the module name (e.g. view/my-namespace/controller/action). I have tweaked the InjectTemplateListener class as follows to fix this:
protected function deriveModuleNamespace($controller)
{
if (!strstr($controller, '\\')) {
return '';
}
~ $module = substr($controller, 0, strpos($controller, '\\Controller'));
+ if (!strstr($module, '\\')) {
+ return $module;
+ }
+ $module = substr( $module, strpos( $module, '\\' ) + 1 );
return $module;
}
|
|
Administrator
|
-- macest <[hidden email]> wrote
(on Thursday, 31 May 2012, 08:00 AM -0700): > > weierophinney wrote > > > > As of beta4, the InjectTemplateListener now injects a template based on > > the controller triggering the event. The rules it follows are: > > > > * It normalizes the top-level (module) namespace of the controller. In > > the case of the Application module, that becomes "application". In > > the case of a module named "FooBar", it becomes "foo-bar". > > > > I have a number of modules namespaced in my modules folder (e.g. > modules\MyNamespace\MyModule) and this functionality makes all their > templates try and load from that name rather than the module name (e.g. > view/my-namespace/controller/action). I have tweaked the > InjectTemplateListener class as follows to fix this: where...? (attachment not showing up on list... can you provide a link?) Also, you can easily provide your own InjectTemplateListener that listens at a higher priority than the default one, for cases such as this -- there's no need to alter the default listener. -- 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] |
|
This post has NOT been accepted by the mailing list yet.
In reply to this post by weierophinney
Matthew,
Thanks for your reply/explanation. It makes sense to me now. I had read one of your blog entries about how you wrote your blog application and I did see yesterday that you were explicitly naming the view script in your controller. I was still confused, though, when comparing those things with what I was seeing and what the ZF2 Skeleton application was doing. For what it's worth, I took another shot at trying to find the documentation on this and couldn't. Not saying it's not there...just trying to show that I tried. ;-) Thanks again... |
|
In reply to this post by weierophinney
|
| Powered by Nabble | Edit this page |
