Search code examples
emailsendgridsendgrid-templates

Combined Handlebars in SendGrid


I'm trying to set up a string match using handlebars in SendGrid. As I understand it, you can only use the if statement with booleans so I'm trying via {{equals}} instead. This works fine but I'm then trying to inject that 'thing' into the result. The result I'm after in the below is that if the data = TEXT then the email reads "This is some TEXT"

{{#equals thing "TEXT"}}
This is some {{insert thing}} 
{{/equals}}

Using {{insert thing}} outside the {{equals}} handlebar is fine but when inside it doesn't return anything. I just get "This is some"

Does anyone know if this is supported or if there is a better way?

Thanks.


Solution

  • I'm not entirely sure how to explain it, but it seems that within blocks in handlebars there is a new scope. This works well for loops, where the scope is the current item in the loop, but less well for conditionals where the scope doesn't seem to include anything. You can get back to the root scope using the @root keyword though, so you can use that to get something from outside of the block. In this case, you can use it like this:

    {{#equals thing "TEXT"}}
    This is some {{insert @root.thing}} 
    {{/equals}}