Twig电子邮件 - 显示和隐藏对象

时间:2016-08-23 15:11:41

标签: php email twig

目前正在使用Twig制作我的电子邮件模板。我在以下情况中遇到了一些困难。

我有2个产品被推送到我的电子邮件模板,但其中一个产品是'礼品包装'。我的想法不是在产品清单中显示GIFT WRAP,而是在摘要区域显示,可以找到SHIPPING,SUBTOTAL和TOTAL。

我可以从列表中隐藏GIFT WRAP,但是在摘要部分存在问题。

这是我到目前为止所做的:

<!-- Summary section -->
{% for item in items %}
   {% if item.title == 'Gift wrap' %}                 
       Show gift wrap
   {% else %} 
       Dont show giftwrap
       But now show another element
   {% endif %}
{% endfor %}

不幸的是'不显示礼品包装'仍然显示。如果有人能帮助我,我将非常感激。

根据要求,这是我模板的一部分

 {% for item in items %}
     {% if item.title == 'Gift wrap' %}                 
          <tr>
             <td style="border-top: 2px solid #202020;"></td>
             <td style="padding-left: 10px; border-top: 2px solid #202020; padding-top: 20px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" width="100px">
                 Subtotal
             </td>
             <td style="border-top: 2px solid #202020; padding-top: 20px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" align="right" width="100px">
                 £{{ subtotal - item.totals.data.rounded.with_tax }}
             </td>
         </tr>
         <tr>
             <td></td>
             <td style="padding-left: 10px; padding-top: 10px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" width="100px">
                  Gift wrap
             </td>
             <td style="padding-top: 10px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" align="right" width="100px">
                  {{ item.totals.data.formatted.with_tax }}
              </td>
          </tr>
     {% else %}
          <tr>
               <td>
                   <p>Add this if gift wrap has not be added to customers order</p>
               </td>
          </tr>
     {% endif %}
 {% endfor %}

1 个答案:

答案 0 :(得分:0)

我猜你想做你想做的事,你需要flag保持Gift wrap是否在项目中。以下是使用flag

解决此问题的方法
{% set has_gift_wrap = false %}
{% for item in items %}
    {% if item.title == 'Gift wrap' %}
        {% set has_gift_wrap = true %}
    {% endif %}
{% endfor %}
 ...
 ...
{% if has_gift_wrap %}                 
    <tr>
     <td style="border-top: 2px solid #202020;"></td>
     <td style="padding-left: 10px; border-top: 2px solid #202020; padding-top: 20px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" width="100px">
         Subtotal
     </td>
     <td style="border-top: 2px solid #202020; padding-top: 20px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" align="right" width="100px">
         £{{ subtotal - item.totals.data.rounded.with_tax }}
     </td>
    </tr>
    <tr>
     <td></td>
     <td style="padding-left: 10px; padding-top: 10px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" width="100px">
          Gift wrap
     </td>
     <td style="padding-top: 10px; font-family: 'Open sans', Helvetica, Arial, sans-serif; color: #666666; font-weight: 300;" align="right" width="100px">
          {{ item.totals.data.formatted.with_tax }}
      </td>
    </tr>
{% else %}
    <tr>
       <td>
           <p>Add this if gift wrap has not be added to customers order</p>
       </td>
    </tr>
{% endif %}

旁注您无法使用{% if "Gift wrap" in items %},因为Gift wrap存储在属性中。