Conditional Content in Message Templates
Conditional content is useful for showing pieces of content that only need to be shown to certain individuals without resorting to trickery or witchcraft when it comes to our Message Templates.
Using conditional content is simple: place your conditional content within the named variable tags, and define the variable name that you used while making an API call, and the conditional content will be included in the email that we send.
Adding Conditional Content to your Templates
{{?variable_name}}
Content here.
{{/variable_name}}
Of course, you can choose any words or phrases or letters for
variable_name, but like any slugs it can't include
spaces or special characters like apostrophes or periods.
Once you've placed your content inside the named tags, you can
make it appear by adding the variable_name to your
list of variables in the API call and simply defining it.
variables: {
'variable_name': 'Any content here.'
}
And that's it! You've made your API call with
variable_name defined, and the conditional content
should be included in the email that we send out for you.
Inverse Conditional Content
Similar to conditional content, inverse conditional content gives you the ability to show content if a variable is not defined. It can be used in conjunction with the conditional content to create an 'else' type of statement.
You define an inverse conditional content section in your
template by using the ?! prefix inside a variable
name.
{{?!variable_name}}
Content here.
{{/variable_name}}
And the content inside between {{?!variable_name}}
and {{/variable_name}} will only appear if
variable_name is not defined or set
to null.
Here is an example of using conditional and inverse conditional to display a block of text that shows the paid status of an account.
Your account status for the month of November:<br />
<br />
{{ ?account_status }}
Paid.
{{ /account_status }}
{{ ?!account_status }}
Not Paid.
{{ /account_status }}
And the corresponding arguments in the API
call:
arguments: {
recipients: {
"email1@example.com": {
'account_status': 'paid'
},
"email2@example.com":{
'account_status': null
}
},
template: "template_name",
};
In this particular example, the account_status
variable will show "Paid." if it is defined, and "Not Paid." if it
isn't defined. Imagine conditional and inverse conditional content
to be similar to an if-else for your mail templates!