具有测试数据和数组的Sendgrid模板

时间:2019-03-18 16:23:13

标签: sendgrid sendgrid-templates

我有一些看起来像这样的测试数据:

{
    "firstName":"Ben",
    "products": [{
      "name": "first product",
      "position": 0
    }, {
      "name": "second product",
      "position": 1
    }, {
      "name": "third product",
      "position": 2
    }]
}

在我的模板中,我有这样的东西:

<p>Hi {{firstName}}</p>

哪个工作正常。 现在我想浏览我的产品

所以我尝试了这个:

{{#each products}}
    {{#if this.position == 0}}
        <h1>{{this.name}}</h1>
    {{else}}    
        <h2>{{this.name}}</h2>
    {{/if}}
{{/each}}

但是它不起作用。 我也找不到有关执行if语句的任何文档。我找到的最接近的是:

https://sendgrid.com/docs/ui/sending-email/using-handlebars/

它讨论了“ Basic If,Else,Else If”,这表明它有一个更高级的版本,但是我找不到它的文档。...

有人知道我在做什么错吗?

PS:我的示例仅针对此帖子进行了简化。

2 个答案:

答案 0 :(得分:0)

似乎SendGrid文档只是使用Basic If...来区别If with a Root的下一部分。他们在页面上方的部分中使用了相似的标题。

基于此,看来它们没有逻辑“如果值等于”,而只有“如果字段存在”,因此您需要更新JSON以使用该格式,例如{ {3}}。

答案 1 :(得分:0)

您需要的是以下结构:

{{#equals city_code avl}}
        AVL
{{else}}
        Not AVL
{{/equals}}

请参见https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#conditional-statements

{{if anything}}仅检查anything是真实的还是虚假的;您无法将它与某些东西进行比较。

相关问题