Using View Templates in Emails. How to more easily apply templates? - php

I've been able to load a view template and use it to create the body of an email message. The code is similar to the answer here:
How to render a mail template with layout in ZF2?
But now I'm looking to take this code and move it into a module that helps the rest of my application send emails. I'm thinking I would like to make it as easy/transparent as possible to use views for all emails.
So the basic question is, how can I setup the code to accept the information it needs to render a template and send the email?
I already have my transport information held inside the service locator, and sending email works fine. My primary concern is mostly the messy code needed for the template resolver:
$view = new PhpRenderer();
$resolver = new TemplateMapResolver();
$resolver->setMap(array(
'mail' => __DIR__ . '/../../../view/communication/email/new-project.phtml'
));
$view->setResolver($resolver);
I am alright with having all the *.phtml for emails held inside the new module. There's no need to send adhoc emails out of the application (we use communications internal to the application).
Is there a way to use the module.config.php 'view_manager' => 'template_map' and bypass the need to create a new resolver()? In that case I could setup the email code to accept the named template that should be in that map.
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),

You simply have to access the default viewrenderer via the ServiceManager.
$viewRenderer = $this->getServiceLocator()->get('viewrenderer');
$mailView = new ViewModel();
$mailView->setTemplate('my-namespace/controller/mailtemplate.phtml');
$mailView->setVariables(array(
//k=>v paired data
));
$renderedOutput = $viewRenderer->render($mailView);
$mail->setBody($renderedOutput);
And that's pretty much it. Obviously though the concrete mail handling is stripped from this example, as you're mostly looking at another problem, given your description.

Related

ZF2 Subdirectory for controllers => view files

I would like to structure my project like the following:
MyModule/src/MyModule/Controller/TestController.php
MyModule/src/MyModule/Controller/Admin/TestController.php
The problem is, that both controllers look for the view file mymodule/test.phtml, because the directory Admin doesn't matter. Is there a way to take care of the directory without write it down manually in each controller action?
I would like to structure it like that:
MyModule/src/MyModule/Controller/TestController.php => view/mymodule/test.phtml
MyModule/src/MyModule/Controller/Admin/TestController.php => view/mymodule/admin/test.phtml
Or maybe someone has an idea to structure it otherwise?
Thanks
I've seen this done in a previous project before. Pretty sure you can config this in the module.config.php file, maybe something like:
// View file paths
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'MyModule/Admin/Test' => __DIR__ . '/../view/mymodule/admin/test.phtml'
)
)
Just make sure the paths are correct. However, you could just have an Admin module and have all of the admin controllers reside there that way you wouldn't have to worry about the Controller/Admin issue you are having with the view files right now. Then all of your other modules could either extend, use DI or the service mgr to get what you need from the Admin module.
You can override the default template injector with your own one where you will specify the logic that will resolve the template path for the admin controllers.
Check out my blog post covering this topic in depth with an example
http://blog.igorvorobiov.com/2014/10/18/creating-a-custom-template-injector-to-deal-with-sub-namespaces-in-zend-framework-2/

Specific Layout for error page in ZF2

I'm learning ZF2 and I am constructing a web site to make sure that I understand the new concepts of ZF2, but I was wondering if there is a way to specify a template for the error pages in the application module.
Thanks
There some pre-set configuration options within the module.config.php like:
...
'view_manager' => array(
'not_found_template' => 'error/404',
'exception_template' => 'error/exception',
'template_map' => array(
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/exception' => __DIR__ . '/../view/error/exception.phtml',
),
),
...
After creating the Folder and the Files you should be good to go.

php Zend framework 2 error 404 page

I was trying to add new error 404 page for my module. I have Application and my own Admin module. For Application I use default 404.phtml, for my new module I created admin404.phtml but I have no idea how to run it. There are a lot of options how to change layout for modules but I couldn`t find answer for my question.
Can anyone help me?
When a page could not be found or some other error happens inside of your web application,
a standard error page is displayed. The appearance of the error page is controlled by the
error templates. There are two error templates: error/404 which is used for "404 Page Not Found" error, and error/index which is displayed when an unhandled exception is thrown somewhere inside of the application.
The module.config.php file contains several parameters under the view_manager key, which you can use to configure the appearance of your error templates:
<?php
return array(
//...
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
//...
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
//...
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index'=> __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
),
);
The display_not_found_reason parameter controls whether to display the detailed
information about the "Page not Found" error.
The display_exceptions parameter defines whether to display information about
an unhandled exception and its stack trace.
The not_found_template defines the template name for the 404 error.
The exception_template specifies the template name for the unhandled exception error.
You typically set the display_not_found_reason and display_exceptions parameters
to false in production systems, because you don't want site visitors see the details
about errors in your site. However, you will still be able to retrieve the detailed
information from Apache's error.log file.

Configure ZF2 view_manager to load 2 separate templates maps

I need 2 different template maps in ZF2 , one for admin and oen for front-end, currently from what I can see ZF2 merges the 2 module.config.php files that are used in the 2 modules I configured, and causes the template map I need to set for the admin, to be loaded in front module also.
the /Application module.config.php
...
'view_manager' => array(
'display_not_found_reason' => true,
'display_exceptions' => true,
'doctype' => 'HTML5',
'not_found_template' => 'error/404',
'exception_template' => 'error/index',
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
'application/index/index' => __DIR__ . '/../view/application/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
'template_path_stack' => array(
__DIR__ . '/../view',
),
...
the /admin module.config.php
...
'view_manager' => array(
'template_path_stack' => array(
'admin' => __DIR__ . '/../view',
),
'template_map' => array(
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
),
),
...
what should I modify so that i can load separate "view_manager" arrays for the 2 separate modules ?
It would be great if you can explain why you are trying to achieve this. As i can see you are trying to have a different layout for admin. Maybe you want to take a look at this module which can already do what you are trying https://github.com/zf-commons/zfcadmin. This module has a layut setup for the admin route.
I too had this problem. I took the approach which is proposed in the below module
https://github.com/EvanDotPro/EdpModuleLayouts
I'm new to ZF2 and I too was looking for an answer on how to have a completely different template for a dashboard, admin and front end.
I used EdpModuleLayouts as suggested here and in many other posts. This solved one aspect of the problem. The layout. I was now able to provide different layouts for the same template which would work well if I were using the same template or wanted to prove a different layout for say forgotten password, registration or a login. But I didn't. I needed a whole different set of folders, css files etc. At this point I could have just nested all templates in to a template folder and pointed the links in the layout files to the appropriate folders. But I didn't want this either.
I also included the zfc-admin module into my app which gave me a clue as to the other aspect which is to provide a different source directly for files. (Uninstalled afterward)
So adding by adding the following to my module_name/config/module.config.php
'view_manager' => array(
'template_path_stack' => array(
__DIR__ . '/../view'
),
),
Enabling EdpModuleLayouts in application.config.php and adding the code below to the Application module.config.php
'module_layouts' => array(
'Application' => 'layout/layout',
'Dashboard' => 'layout/dashboard',
'Admin' => 'layout/admin',
),
This is probably not the best way to do it but it worked. The only issue I could really see with doing it this way is that EdpModuleLayouts wants to pull all the layouts from the Application/view/layout folder. It did however allow me to keep all my module template files in the view section of the module being worked on.

how to use special layouts in zend framework 2?

i have a bare bones zf2 application and i have 2 modules (Users and Album).
They both have their own layouts but i want to use the layout from Album for the Users, just to keep them a bit more consistent.
Im not sure what is the best way to delegate what layout a module should use.. from the controller __contruct, or on each individual action or in the module.config.php.
maybe here:
'template_map' => array(
'layout/top_nav' => __DIR__ . '/../view/layout/top_nav.phtml',
'layout/layout' => __DIR__ . '/../view/layout/layout.phtml',
^^ probably i should set this path to the layout i want to use ^^
'posts/index/index' => __DIR__ . '/../view/posts/index/index.phtml',
'error/404' => __DIR__ . '/../view/error/404.phtml',
'error/index' => __DIR__ . '/../view/error/index.phtml',
),
any ideas? maybe the best practices?
thanks
edit: i just did this and it worked:
'layout/layout' => __DIR__ . '/../../album /view/layout/layout.phtml',
but i'm still not sure if this is best practice.
maybe i can setup multiple template_map arrays and use them in the User controller, in my case
Edit: removed my own bullshit, just see the alternative approach :) That works
Alternative approach
Evan Coury also made the effor to have the Layout configuration globally available. This is especially true for single developers who won't make their modules public, ever. You will find his EdpModuleLayouts Module right here.
Using that module switching layouts becomes even simpler like the following:
<?php
array(
'module_layouts' => array(
'ModuleName' => 'layout/some-layout',
),
);
Hope it helps.

Categories