使用 Symfony Mailer 在 Twig 的模板化电子邮件中嵌入 Webpack 资产 [英] Embedding a Webpack asset inside Twig's templated e-mail using Symfony Mailer
问题描述
Symfony 的邮件文档 说可以轻松嵌入图像:
The Symfony's mailer documentation says one can embed an image easily:
<img
src="{{ email.image('@images/logo.png') }}"
alt="Logo">
但是我的所有资产都是由 Webpack & 构建的.再来一次.我也使用资产清单文件,因为每个资产的名称中都有一个 chunkhash.
However I have all my assets built by Webpack & Encore. I use the asset manifest file as well, because every asset has a chunkhash in its name.
在这种情况下如何使用 Twig 嵌入图像?我试过了:
How to embed an image using Twig in this case? I tried:
<img
src="{{ email.image(asset('build/images/logo.png')) }}"
alt="Logo">
但这行不通.我最终得到一个模板丢失"的异常.如果我使用来自静态位置的图像,这当然很有效.
But this doesn't work. I end up with an exception that "a template is missing". This works well of course if I use an image from a static location.
推荐答案
正如文档所说:定义一个 Twig 命名空间,指向您的图像存储在的任何目录,例如:
As documentation says: define a Twig namespace that points to whatever directory your images are stored in, for example:
# config/packages/twig.yaml
twig:
paths:
# point this wherever your images live
public/build/: build
接下来,在您的电子邮件模板中:
next, in your email template:
{# '@build/' refers to the Twig namespace defined earlier #}
<img src="{{ email.image('@build/logo.png') }}" alt="Logo">
<img src="{{ email.image('@build/subdir/logo.png') }}" alt="Logo">
如果构造的图像具有一些随机后缀,则这将不起作用,例如当 .enableVersioning()
在 webpack.config.js
配置文件中定义时.
this will not work if the constructed image has some random suffix, e.g. when .enableVersioning()
is defined in webpack.config.js
config file.
如果您启用了版本控制策略,您可以执行以下操作:
If you have enabled the versioning strategy, you can do the following:
# config/packages/twig.yaml
twig:
paths:
public/: public
并使用字符串连接运算符 ~
构建模板名称:
and build the template name using the string concat operator ~
:
{# '@public' refers to the Twig namespace defined earlier #}
<img src="{{ email.image('@public' ~ asset('build/logo.png')) }}" alt="Logo">
这篇关于使用 Symfony Mailer 在 Twig 的模板化电子邮件中嵌入 Webpack 资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!