覆盖模板电子邮件FOSUserBundle

时间:2018-06-12 13:53:30

标签: symfony templates twig fosuserbundle

https://symfony.com/doc/current/bundles/FOSUserBundle/emails.html

此链接显然解释了如何从FOSUserBundle覆盖电子邮件模板以重置用户的密码。

我有一个重置电子邮件的新文件(之前是@FOSUser/Resetting/email.txt.twig),现在抛出config.yml我可以告诉FOSUserBundle使用另一个文件。

fos_user:
    service:
        mailer: fos_user.mailer.twig_swift
    resetting:
        email:
            template:   'email/password_resetting.email.twig'

在链接中说如果我添加" mailer:fos_user.mailer.twig_swift"将有可能处理HTML代码。

这个新文件我需要添加一个HTML代码,所以我按照文档中的说法进行了尝试:

使用或不使用" autoescape"在{%block body_html%}中添加所有html代码 - >相同的结果......我可以看到所有的html标签......

我做错了什么?

例如:

{# app/Resources/views/email/password_resetting.email.twig #}

{% block subject %}Resetting your password{% endblock %}

{% block body_text %}
    {% autoescape false %}
        Hello {{ user.username }} !

        You can reset your password by accessing {{ confirmationUrl }}

        Greetings,
        the App team
    {% endautoescape %}
{% endblock %}

{% block body_html %}
    {#
        //You can of course render the html directly here.
        //Including a template as done here allows keeping things DRY by using
        //the template inheritance in it
    #}
    <p><b>Test</b> test test</p>
    {{ '<p><b>Test</b> test test</p>'|raw }}

    {% include 'email/password_resetting.html.twig' %}
{% endblock %}

来自email / pasword_resetting.html.twig的内容是:

<p><b>Test</b> test test</p>
{{ '<p><b>Test</b> test test</p>'|raw }}

我得到了:

  Hello ricard !

    You can reset your password by accessing https://blablabla.bla/app_dev.php/es/resetting/reset/MiPqznsUxHQLLgviDYtCsJrQZBiaqVzDU5ENvHcadA

    Greetings,
    the App team

        <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
<p><b>Test</b> test test</p>

我希望看到段落句子的粗体和格式,而不是标签。

我也试过了:

{# app/Resources/views/email/password_resetting.email.twig #}

{% block subject %}Resetting your password{% endblock %}

{% block body_text %}
    {% autoescape false %}
        Hello {{ user.username }} !

        You can reset your password by accessing {{ confirmationUrl }}

        Greetings,
        the App team
    {% endautoescape %}
{% endblock %}

{% block body_html %}
    {#
        //You can of course render the html directly here.
        //Including a template as done here allows keeping things DRY by using
        //the template inheritance in it
    #}
    {% autoescape 'html' %}
        <p><b>Test</b> test test</p>
        {{ '<p><b>Test</b> test test</p>'|raw }}
    {% endautoescape %}
    {% autoescape %}
        <p><b>Test</b> test test</p>
        {{ '<p><b>Test</b> test test</p>'|raw }}
    {% endautoescape %}
    <p><b>Test</b> test test</p>
    {{ '<p><b>Test</b> test test</p>'|raw }}

{% endblock %}

我得到了:

Hello ricard !

    You can reset your password by accessing https://blablabla.bla/app_dev.php/es/resetting/reset/2G2ZGW262Z1THu1_80k2vAQMdI4-faNFVFWgdOVts8

    Greetings,
    the App team

            <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
            <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
    <p><b>Test</b> test test</p>
<p><b>Test</b> test test</p>

1 个答案:

答案 0 :(得分:0)

好吧,终于可以找到答案了。

我正在重写ResettingController,并在services.yml中声明了对该新控制器的依赖项注入,而不是FOSUserBundle中的默认控制器。

所以我得到了

AppBundle\Controller\ResettingController:
    class: AppBundle\Controller\ResettingController
    arguments: ['','@fos_user.resetting.form.factory', '','','@fos_user.mailer.default', '']

替换为:

AppBundle\Controller\ResettingController:
    class: AppBundle\Controller\ResettingController
    arguments: ['','@fos_user.resetting.form.factory', '','','@fos_user.mailer.twig_swift', '']