简体   繁体   中英

Symfony2 / Twig: How to force rerender of single template?

I have a Symfony project which also sends some standard emails like "Dear user XYZ, please find the attached file" etc. - the content of these emails is stored in Twig-templates like yourfile.html.twig. The admin has the ability to change those basically "text-files" in a simple webinterface. Any changes are written back to the file. The emails are send with SwiftMailer just like in the example code in their docs with the filepath to the twig template.

This works fine in the debug environment - as Twig automatically rerenders cached files which were changed. However, when I am in production environment this doesn't work anymore. Changes made to the mail template aren't recognised by Twig and it uses the cached version (and the user therefore gets an email with the text of the old version).

One way to solve this problem would be to just clear the cache every time the admin changed the email templates (which won't happen too often). Anyway, I don't like this way as it forces a full rerender of every page which is just a waste of resources...

Another possibility would be to enable the "auto rerender" feature of Twig also in production environment, but again I'm not happy with this solution as it results in performance loses...

The "best" way of doing this would be to call some function to rerender just this one file which was changed (Pseudo code: twig->rerender("path/to/file/yourfile.html.twig")), but I could not find any way to accomplish this. Maybe I just used the wrong keywords for searching?

I would really appreciate if someone has an idea on how to solve the problem above!

Thanks in advance, katze_sonne

You can store text of your letter in database. And in twig for example you will have only {{letter}} . And your problem will disappear. I think that this way is more useful. And you don't need rewrite file in project folder, on server may be some problems with it.

Another variant you can send email from command. Command always execute in dev environment so it will get non cached copy of your files. If you send letter from controller you can use How can I run symfony 2 run command from controller .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM