最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

amazon web services - How can I do conditional content in an SES template with handlebars? - Stack Overflow

programmeradmin0浏览0评论

I was looking at this article:

.html

and then created a bunch of conditionals in my template like:

{{#if foo}}
  <p>{{ foo }}</p>
{{/if}}
{{#if bar}}
  <p>{{ bar }}</p>
{{/if}}

but it seems that if I do not provide foo and bar in my template data, the email does not get sent due to a render error. I also tried doing:

{{#if data.foo}}
  <p>{{ data.foo }}</p>
{{/if}}
{{#if data.bar}}
  <p>{{ data.bar }}</p>
{{/if}}

and giving { data: {} } to the template data.. That does not work either.

How can I actually have a template that allows optional content?

I was looking at this article:

https://docs.aws.amazon/ses/latest/dg/send-personalized-email-advanced.html

and then created a bunch of conditionals in my template like:

{{#if foo}}
  <p>{{ foo }}</p>
{{/if}}
{{#if bar}}
  <p>{{ bar }}</p>
{{/if}}

but it seems that if I do not provide foo and bar in my template data, the email does not get sent due to a render error. I also tried doing:

{{#if data.foo}}
  <p>{{ data.foo }}</p>
{{/if}}
{{#if data.bar}}
  <p>{{ data.bar }}</p>
{{/if}}

and giving { data: {} } to the template data.. That does not work either.

How can I actually have a template that allows optional content?

Share asked Nov 19, 2024 at 19:58 patrickpatrick 9,75213 gold badges65 silver badges126 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Every variable referenced in your template must exist in the payload, even if the value is null or an empty string.

Before sending your payload to SES, ensure default values are set for missing variables:

{
  "foo": null,
  "bar": null
}

Like that you ensure that for example foo and bar will always be there, and you will not get rendering issue.

that's happening because AWS SES expects every referenced variable (even within {{#if}}) to be defined in the input payload. If data.foo or data.bar doesn’t exist in the payload (even if it’s referenced within a conditional), SES throws a rendering error.

So even optional variables, need to be defined in the payload to avoid rendering errors.

so if you're creating the payload somehow using code, you can update it to something like this:

const data = {
  foo: userInput.foo || "",
  bar: userInput.bar || null
};
发布评论

评论列表(0)

  1. 暂无评论