扩展模板不适用于 FOSUserBundle 电子邮件.Symfony2.



我在电子邮件和扩展模板方面遇到问题。我正在尝试为电子邮件构建一个模板,然后在不同的捆绑包中为电子邮件使用一个模板。我自己的捆绑包我成功地实现了这个,但是当我尝试扩展FosUserBundle时出现了问题:重置电子邮件。

我的模板在应用程序/资源/视图/email_base.html.twig 中

{% block subject %}{% endblock subject %}
<div dir="ltr" style="display: block; width: 100%; background: #ffffff">
    <div class="gmail_quote">
        <div style="margin:0;padding:20px 0; background: #ffffff">
            <table align="center" bgcolor="#F9F9F9" cellpadding="0" width="600" style="border-spacing:20px 0;color:#4d4d4d;font-family:Arial,Helvetica">
                <tbody>
                    TROLLOLOLL
                    {% block body_html %}
                    {% endblock body_html %}
                </tbody>
            </table>
            <table align="center" bgcolor="#FFFFFF" width="600" style="margin-top:12px;color:#bdbdbd;font-family:Arial,Helvetica;font-size:11px;text-align:justify">
                <tbody>
                    <tr>
                        <td colspan="4" style="padding-top:10px">
                            This e-mail is intended solely for the addressee, may contain proprietary and legally privileged information. 
                            If you have received this e-mail by mistake please notify the sender and delete this e-mail along with all attachments. 
                            Thank you.
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

我尝试使用模板扩展的电子邮件位于UserBundle/Resources/views/Reset/resetting_email.html.twig中,如下所示:

{% extends '::email_base.html.twig' %}
{% block subject %}Resetting password
{% endblock %}
{% block body_html %}
    <tr>
        <td colspan=2 style="text-align: left;">
            <h3>Witaj {{ user.username }}</h3>
        </td>
    </tr>
    <tr>
        <td colspan=2>
            <p>Jeśli chcesz zresetować hasło, kliknij w następujący link <a style="color: #EA2227; text-decoration: underline" href="{{ confirmationUrl }}">reset hasła</a>.</p>
        </td>
    </tr>
{% endblock body_html %}

问题是,当我发送此消息时,我只能看到来自 resetting_email.html.twig 的内容。而且根本没有来自 base_email.html.twig 的内容。

在我的配置文件中,我设置了重置文件resetting_email.html.twig的模板:

fos_user:
    db_driver: orm 
    firewall_name: main
    user_class: GLUserBundleEntityUser
    from_email: 
        address:  development@##########.pl
        sender_name:  Admin ##########
    resetting:
        email:
            template:   GLUserBundle:Resetting:resetting_email.html.twig
    service:
        mailer: fos_user.mailer.twig_swift

您必须将电子邮件封装到一个块中才能呈现。

让我们来看看 FOSUSerBundle Twig mailer 类:

protected function sendMessage($templateName, $context, $fromEmail, $toEmail)
{
    $template = $this->twig->loadTemplate($templateName);
    $subject = $template->renderBlock('subject', $context);
    $textBody = $template->renderBlock('body_text', $context);
    $htmlBody = $template->renderBlock('body_html', $context);
    $message = Swift_Message::newInstance()
        ->setSubject($subject)
        ->setFrom($fromEmail)
        ->setTo($toEmail);
    if (!empty($htmlBody)) {
        $message->setBody($htmlBody, 'text/html')
            ->addPart($textBody, 'text/plain');
    } else {
        $message->setBody($textBody);
    }
    $this->mailer->send($message);
}

这些行是问题的关键:

    $subject = $template->renderBlock('subject', $context);
    $htmlBody = $template->renderBlock('body_html', $context);
模板

不会像我们在从控制器渲染的典型模板中习惯的那样作为一个整体呈现。它将逐块渲染,这是一个很酷且有趣的技巧。

因此,要使您的电子邮件正常工作,您必须重新定义body_html块,以便正确呈现

{% block body_html %}
<div dir="ltr" style="display: block; width: 100%; background: #ffffff">
...
</div>
{% endblock body_html %}

我遇到了同样的问题,我需要使用基本邮件模板。在块{% block body_html %}内,我嵌入了嵌入了一个很棒的 twig 命令的基本模板。这允许我将我的模板用作带有适当块的布局(就像extends一样)。

{% block body_html %}
    {% embed '@MyCompany/Mails/Grouped/base.html.twig' %}
        {% set header = "some variable defined in the template" %}
        {% block content %}
            Confirm your email bla-blah-blah
        {% endblock %}
    {% endembed %}
{% endblock %}

最新更新