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 badges1 Answer
Reset to default 0Every 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
};