Using latte templates for e-mail messages in Nette 2.2

5 years ago

duke
Member | 639
+
0
-

In Nette 2.2 the interface Nette\Templating\ITemplate as well as the class Nette\Templating\FileTemplate are marked as deprecated. What is the suggested new way of using latte templates for e-mail messages in Nette 2.2? I.e. what is the new way of doing this?

5 years ago

Tomáš Votruba
Moderator | 1154
+
0
-

I'd recommend using presenter

5 years ago

mystik
Member | 77
+
0
-

Tomáš: Is there any other recommended way to do that? Way which does not require dependency from mail-generating service on Presenter or Application?

I'm currently using this code:

<?php
  $latte = new \Latte\Engine();
  $template = new \Nette\Bridges\ApplicationLatte\Template($latte);
  $template->setFile("example.latte");
?>

But I'm not sure if this is correct solution. Any ideas?

Last edited by mystik (2014-05-14 12:58)

5 years ago

Tomáš Votruba
Moderator | 1154
+
0
-

@mystik: It depends, what logic do you want to use in your templates. Latte itself could be enought, but maybe you need more Application related featues, like some filters, $basePath, User etc.

Solution you mentioned is correct if suits your needs.

tl/dr

  1. If you can, use application. It easy, fast and realiable solution.
  2. If you cannot, use Latte itself like mentioned here.

5 years ago

duke
Member | 639
+
0
-

I've noticed one problem when using Application and cloning the template object of Presenter. Such template is instance of Nette\Application\UI\ITemplate, and not instance of Nette\Templating\ITemplate and as can be seen from this line, certain useful variables (such as $mail) won't be automatically defined in the template by Nette\Mail\Message::setHtmlBody() then. Strangely enough, the API documentation contains different line (which I was unable to find in git history at all) that would make it work…

5 years ago

Quinix
Member | 108
+
0
-

That line is only in v2.2 branch – https://github.com/…eea8b0c96d2b

If you dont wanna touch application or presenter, you can inject Nette\Application\UI\ITemplateFactory and create templeta by

$template = $templateFactory->createTemplate();

It's the same way presenter creates templates in 2.2.

Last edited by Quinix (2014-05-28 22:49)

5 years ago

duke
Member | 639
+
0
-

@Quinix Thanks for pointing that out. The ITemplateFactory::createTemplate() however requires an instance of Nette\Application\UI\Control as an argument…

But anyway, it is usually useful to be able to use {link} or {plink} macros in the message template, for which it seems necessary to work with presenter.