2013-10-30 50 views
4

我有電子郵件和擴展模板的問題。我正在嘗試爲電子郵件構建一個模板,然後在不同的Bundles中爲這些電子郵件使用一個模板。我自己的包,我sucesfully成就了這一點,但是當我嘗試擴展FosUserBundle時出現問題:重置電子郵件。將擴展模板與FOSUserBundle電子郵件不兼容。 Symfony2

我的模板在app /資源/視圖/ 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 propriertary 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 /資源/視圖/復位/ resetting_email.html.twig看起來像:

{% extends '::email_base.html.twig' %} 

{% block subject %}Resseting 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: GL\UserBundle\Entity\User 

    from_email: 
     address: [email protected]##########.pl 
     sender_name: Admin ########## 

    resetting: 
     email: 
      template: GLUserBundle:Resetting:resetting_email.html.twig 
    service: 
     mailer: fos_user.mailer.twig_swift 

有人可以解釋這樣對我,還是說明了什麼?我會非常不滿意

回答

2

你必須把你的電子郵件放在一個塊中,以便它被渲染。

讓我們來看看在FOSUSerBundle Twig mailer class

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 %} 
+0

感謝,這解決了這個問題 – user2576422

2

我有同樣的問題,我需要使用基本的郵件模板。 {% block body_html %}塊內嵌入了一個美妙的樹枝命令embed基本模板。這允許我使用我的模板作爲佈局(如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 %} 
+0

我剛纔想這 # {%塊body_html%} { 嵌入% '::電子郵件/ base.html.twig' %} { %塊含量% } {%endblock%} {%endembed} { %} {%endblock%} # 但我仍然只在text/plain電子郵件中獲得文本和html:/ – Bizmate