我正在尝试理解Twig的FOSUserBundle实现和Symfony2的相对新手 . 我想覆盖默认实现并使用fos_user.mailer.twig_swift邮件服务发送html电子邮件 .
我的环境:Symfony 2.4.1
"require": {...
"symfony/swiftmailer-bundle": "~2.3",
"friendsofsymfony/user-bundle": "*"
这里's where I'被卡住了:基于FOSUserBundle文档(here) . 杀死我的那条线是:
{%include'AcmeDemoBundle:User:resetting_email.html.twig'%}
按照他们的指示我得到:
" Unable to find template "PP2UserBundle:User:resetting_email.html.twig" - (PP2UserBundle is my implementation of AcmeDemoBundle)
这个错误几乎对我有意义 . 我不明白它会在哪里找到该模板 . 因此,为了防止崩溃,我删除了包含并添加一些HTML代码来测试它是否看到我的模板并发送电子邮件 . 它确实和 {% block body_html %}
中的"Hi"收到了一封很好的电子邮件 . {% block subject %}
也渲染 . 但 {% block body_text %}
是空的 . 钱币 .
这是我的代码 . 如果有人能分享一些见解,我真的很感激!
服务已注册,看起来不错 . (虽然我对yml实现有疑问)但我从另一篇文章中选择了xml-> yml的翻译 .
services.yml
fos_user.mailer.twig_swift:
class: FOS\UserBundle\Mailer\TwigSwiftMailer
arguments:
- @mailer
- @router
- @twig'
- { template: { confirmation: %fos_user.registration.confirmation.template%, resetting: %fos_user.resetting.email.template% }, from_email: { confirmation: %fos_user.registration.confirmation.from_email%, resetting: %fos_user.resetting.email.from_email% } }
Config.yml看似合乎逻辑 .
service:
mailer: fos_user.mailer.twig_swift
resetting:
email:
template: PP2UserBundle:User:resetting.email.twig
我的模板放在Resources / views / User中
resetting.email.twig
{% block subject %} Test Resetting your password{% endblock %}
{% block body_text %}
{% autoescape false %}
Hello {{ user.username }} !
You can reset your email by accessing {{ confirmationUrl }}
Greetings,
the Acme team
{% endautoescape %}
{% endblock %}
{% block body_html %}
<h1> hi </h1>
{% include 'PP2UserBundle:User:resetting_email.html.twig' %}
{% endblock %}
再次,如果我拿出{%include'PP2UserBundle:User:resetting_email.html.twig'%}我得到了一个很好的小'嗨',但很少 .
感觉我应该在视图/重置,(或包括它们)中扩展fosuser模板,但我看不出哪个 . 我错过了什么 .
再次感谢 .
1 回答
您可以使用twigs embed标记来使用自己的电子邮件模板 .
例如,您在Acme中有一个模板:电子邮件:email.html.twig:
然后,您可以将此模板文件嵌入resetting.email.twig并插入body_text块: