Reputation: 213
I have used a lot of the email marketing platforms, but I don't think I am understanding the developer guidelines for using Handlebar.js with SendGrid to use If/Then statements.
I know my way around the code, but the guide isn't clear how to structure the statement for use with their custom fields. It looks like there needs to be some sort of path. Or maybe not. The examples they use don't really help.
https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#basic-if-else-else-if
I've basically rewritten the code over and over and sent myself some tests. I can get the field to show up without the if-then statement, but other than that its a no go. I have also written tech support, but have not heard back yet. No chat feature :/
{{#if city_code=avl}}
AVL
{{else}}
not avl
{{/if}}
The email only contains the {{else}} part of the code. So in the above example "not avl".
Upvotes: 1
Views: 2546
Reputation: 21
I hope you already found an answer to your question though Sendgrid does not allow to extend Handlebars with extra plug-ins contrary to suggested above. What you need is the following construct,
{{#equals city_code avl}}
AVL
{{else}}
Not AVL
{{/equals}}
https://sendgrid.com/docs/for-developers/sending-email/using-handlebars/#conditional-statements
{{if anything}} - only check whether anything is truthy or falsy, you cannot compare it to something.
Upvotes: 2
Reputation: 3559
The if statement verify only if the variable is true.
In order to achieve your example, you should create a new helper:
Handlebars.registerHelper("eq", function(a, b, options){
if (a == b) {
return options.fn(this);
}else{
try{
return options.inverse(this);
}catch(e){}
}
});
And your handlebar template will be:
{{#eq city_code avl}}
AVL
{{else}}
not avl
{{/eq}}
Upvotes: 0