使用 liquid 想要遍历 JSON 对象列表并将代码附加到除倒数第二个对象之外的所有对象



我正在创建一个事务性电子邮件并读取包含产品信息(产品图像和描述(的 JSON 对象,并在电子邮件中显示这些内容。对于我正在显示的每个元素,我在循环遍历对象时附加 HTML 代码(填充(。我想遍历 JSON 对象,但对于最后一个元素,不要附加额外的 HTML 代码。

我目前已经设置了一个 for 循环,用于读取产品并附加一个表行,该行在从 JSON 对象调用的每个产品的末尾显示一行。我找不到一种方法让这条线不出现在最终对象上。

{% for products in merge_vars.order.products %}
{% for products in merge_vars.order.products %}
<!-- TOP CARD -->
<table>
<tr>
<td colspan="3" bgcolor="#FFFFFF" width="100%" height="20"
style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;">&nbsp;
</td>
</tr>
<tr>
<!-- IMAGE -->
<td class="img-small" align="left" width="80" valign="top" style="vertical-align: top; padding: 0 20px;">
<img alt="{{ products.name }}" src="{{ products.image }}" width="80" height="80" border="0"
style="width:80px; height:80px;">
</td>
<!-- IMAGE END -->
<!-- PRODUCT DESCRIPTION -->
<td align="left" width="210" style="padding: 0 20px 0 0;">
<table width="100%" cellpadding="0" cellspacing="0" border="0" align="left">
<tr>
<td align="left" style="font-size: 13px; line-height: 20px; letter-spacing: 0.4px; font-family: Verdana, Geneva, sans-serif; color: #666666; font-weight:normal;">
<span style="color: #868686;">
{{ products.description }}
</span>
</td>
</tr>
</table>
</td>
</tr>
<!-- LINE and Bottom Padding -->
<tr>
<td colspan="3" bgcolor="#FFFFFF" width="100%" height="20"
style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;">&nbsp;
</td>
</tr>
<tr>
<td colspan="3" bgcolor="#F7F7F7" width="100%" height="2"
style="font-size:1px; line-height:1px; mso-line-height-rule: exactly;">&nbsp;
</td>
</tr>
<!-- LINE END -->
</table>
<!-- TOP CARD END -->
<!-- ADDITIONAL PRODUCTS END -->
{% endfor %}

因此,当我浏览每个对象时,我得到了预期的底部填充和线条,但我无法让这个填充和线条不出现在最后一个元素上。

你可以这样实现:

{% unless forloop.last %}
HTML Code you do not want to iterate for the last item in the loop.
{% endunless %}

相关内容

最新更新