Symfony2 / Twig:如何强制重新呈现单个模板?

时间:2021-09-03 07:02:22

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.

我有一个Symfony项目,它还发送一些标准的电子邮件,如“亲爱的用户XYZ,请找到附件”等 - 这些电子邮件的内容存储在像youfile.html.twig这样的Twig模板中。管理员可以在简单的Web界面中更改那些基本上“文本文件”。任何更改都会写回文件。电子邮件与SwiftMailer一起发送,就像在文档中的示例代码中一样,文件路径指向twig模板。

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).

这在调试环境中工作正常 - 因为Twig会自动重新呈现已更改的缓存文件。但是,当我在生产环境中时,这不再起作用了。 Twig无法识别对邮件模板所做的更改,并且它使用缓存版本(因此用户会收到包含旧版本文本的电子邮件)。

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...

另一种可能性是在生产环境中启用Twig的“自动重新渲染”功能,但是我对此解决方案不满意,因为它会导致性能损失......

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?

执行此操作的“最佳”方法是调用某个函数来重新渲染这个已更改的文件(伪代码:twig-> rerender(“path / to / file / yourfile.html.twig”)),但我无法找到任何方法来实现这一目标。也许我只是使用错误的关键字进行搜索?

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

如果有人知道如何解决上述问题,我将非常感激!

Thanks in advance, katze_sonne

提前谢谢,katze_sonne

1 个解决方案

#1


0  

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.

您可以将信件的文本存储在数据库中。例如,在树枝中,您将只有{{letter}}。你的问题就会消失。我认为这种方式更有用。而且你不需要在项目文件夹中重写文件,在服务器上可能会遇到一些问题。

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 .

另一种变体可以从命令发送电子邮件。命令总是在开发环境中执行,因此它将获得文件的非缓存副本。如果您从控制器发送信件,您可以使用如何从控制器运行symfony 2运行命令。

#1


0  

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.

您可以将信件的文本存储在数据库中。例如,在树枝中,您将只有{{letter}}。你的问题就会消失。我认为这种方式更有用。而且你不需要在项目文件夹中重写文件,在服务器上可能会遇到一些问题。

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 .

另一种变体可以从命令发送电子邮件。命令总是在开发环境中执行,因此它将获得文件的非缓存副本。如果您从控制器发送信件,您可以使用如何从控制器运行symfony 2运行命令。