代码之家  ›  专栏  ›  技术社区  ›  Ricard Espinàs Llovet

覆盖模板电子邮件FosUserBundle

  •  0
  • Ricard Espinàs Llovet  · 技术社区  · 7 年前

    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代码,所以我按照文档中的说明进行了尝试:

    添加%块体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 回复  |  直到 7 年前
        1
  •  0
  •   Ricard Espinàs Llovet    7 年前

    好吧,我终于找到答案了。

    我正在重写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', '']