Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic Template in SendGrid: check a string

I'm creating a dynamic template in SendGrid. I want to change the color of a field based on a field in my case Success.

For example, this is my json.

{
    "Environment": "[DEV] ",
    "RunAtTime": "09/06/2020 11:29:02 +01:00",
    "Status": "Success",
    "OrganisationId": "6",
    "OrganisationName": "Test",
    "InvoiceId": "2",
    "InvoiceRef": "Acme Ltd., A1 Taxis",
    "Amount": "50.00"
}

Following the documentation, I tried to do something like

{{#if this.Status = 'Success'}}
#00b300
{{else}}
#ff0000
{{/if}}

I found example only for boolean field.

My goal is to change the text color based on a field. What is the best way to do that?

like image 618
Enrico Rossini Avatar asked Sep 03 '25 09:09

Enrico Rossini


2 Answers

Sendgrid doesn't allow to check strings like this Please modify code as below

{{#equals this.Status "Success"}}

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

like image 59
larry Avatar answered Sep 04 '25 23:09

larry


See the complete example below where if and equals are combined:

{{#if Status}}

    {{#equals Status "Success"}}
        do_something
    {{/equals}}

    {{#equals city "Failed"}}
        do_something
    {{/equals}}

{{else}}
    do_something
{{/if}}

Also check the other statements:

Conditional statements:
{{#if variable}}
{{#unless variable}}
{{#greaterThan variable value}}
{{#lessThan variable value}}
{{#equals variable value}}
{{#notEquals variable value}}
{{#and variable1 variable2}}
{{#or variable1 variable2}}

Looping statements:
{{#each hash}}
like image 27
Vladutz Avatar answered Sep 05 '25 01:09

Vladutz