【问题标题】:Sendgrid templates with test data and arrays带有测试数据和数组的 Sendgrid 模板
【发布时间】:2019-08-09 02:02:46
【问题描述】:

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

{
    "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:我的示例仅针对这篇文章进行了简化。

【问题讨论】:

    标签: sendgrid sendgrid-templates


    【解决方案1】:

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

    基于此,它们似乎没有“如果值等于”的逻辑,只有“如果字段存在”,所以你需要更新你的 JSON 以使用该格式,例如它们在 @ 987654322@.

    【讨论】:

      【解决方案2】:

      您需要的是以下构造:

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

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

      {{if anything}} 只检查anything 是真还是假;你不能把它和什么东西比较。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2022-07-12
        • 2017-05-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多