嵌套变量未使用小胡子在电子邮件模板中呈现

Nested variables not rendering in email template using mustache

提问人:user2382321 提问时间:5/21/2022 更新时间:10/26/2022 访问量:327

问:

因此,我无法在电子邮件模板中显示变量值。第三方电子邮件模板提供商是 Postmark,它使用 Mustache。我的模板是这样设置的(我省略了一些不相关的html以使事情更短):

{{#discount_group.delivery_fee}}
<tr>
  <td width="30%" class="purchase_footer" valign="middle">
    <p class="purchase_total">{{delivery_fee}}</p>
  </td>
</tr>
{{/discount_group.delivery_fee}}
{{#discount_group.discount}}
<tr>
  <td width="30%" class="purchase_footer" valign="middle">
    <p class="purchase_total">{{discount}}</p>
  </td>
</tr>
<tr>
  <td width="30%" class="purchase_footer" valign="middle">
    <p class="purchase_total_bold">{{grandtotal}}</p>
  </td>
</tr>
{{/discount_group.discount}}

我的 json 有效负载如下所示:

"discount_group": {
  "delivery_fee":"delivery_fee_Value",
  "discount": "discount_Value",
  "grandtotal": "grandtotal_Value"
}

但是当我发送电子邮件时,各部分呈现正确,但变量值为空(红框):enter image description here

如果我从 json 有效负载中删除“delivery_fee”,则该部分不会按预期呈现,但值缺失:enter image description here

我也尝试过等等,但仍然有缺失的值。{{discount_group.delivery_fee}}{discount_group.discount}}

我做错了什么? 提前致谢

HTML 胡须 邮戳

评论


答:

0赞 user2382321 5/23/2022 #1

所以我想通了。我不确定为什么它必须这样,但它确实如此。我的问题在于有效载荷。有效负载的格式应如下所示:

"discount_group": {
  "delivery_fee":{
    "delivery_fee":"delivery_fee_Value"
  },
  "discount": {
    "discount":"discount_Value",
    "grandtotal": "grandtotal_Value"
  }
}
0赞 Lia 10/26/2022 #2

当你用小胡子包装一个代码块时,你要做的就是单步执行数据中的该对象,以使你的代码更具可读性。邮戳文档称其为“范围界定”。你可以在这里阅读!

因此,例如,通过以 {{#discount_group.delivery_fee}} 开头的块,您已经处于delivery_fee,再次调用它将不返回任何内容,因为它不存在。

根据数据的原始结构,您需要的所有内容都嵌套在 discount_group 中,因此无需进一步嵌套在括号中。我知道你已经找到了解决方案,但是将来,你可以考虑将代码更新为如下所示,而不是更改数据以匹配代码:

{{#discount_group}}
<tr>
  <td width="30%" class="purchase_footer" valign="middle">
    <p class="purchase_total">{{delivery_fee}}</p>
  </td>
</tr>
<tr>
  <td width="30%" class="purchase_footer" valign="middle">
    <p class="purchase_total">{{discount}}</p>
  </td>
</tr>
<tr>
  <td width="30%" class="purchase_footer" valign="middle">
    <p class="purchase_total_bold">{{grandtotal}}</p>
  </td>
</tr>
{{/discount_group}}