A Conditional statement allows you to dynamically determine which content will display in an email for a recipient based on information stored in CRM. For example, you can display specific content in an email template based on the recipient's gender.
Goals
- Learn how to use Conditional Statements in an Email Template to expand the template's flexibility.
- Learn when to use if, elseif, and else statements within conditional statements
The Basics
The standard steps involved in setting up a conditional statement are:
- assign variables
- check conditions
- else scenario
- end statement
Here is an example of what a standard conditional statement looks like:
<#assign gender=Recipient.contact.gendercode[0]!"null"/>
<#if gender=="Male">
This content will display for men.
<#elseif gender=="Female">
This content will display for women.
<#else>
This content will display for recipients of unspecified gender.
</#if>
When setting up a conditional statement you must begin by assigning a variable for any fields you want to reference using the #assign Freemarker Tag. At this point, the values for those variables are checked, a condition is included to account for edge cases, and the conditional statement is closed out. This holds true regardless of how many conditions are being checked or how many variables are being used.
NOTE: Variables must be one word, otherwise they will throw a render error when you try to send the email. If you want to dynamically reference a CRM field whose name is more than one word, like Marital Status, you could represent that as a variable name as maritalstatus, marital_status, or any other style that removes the space between words. The variable name does not have to match the CRM field name, but we do recommend at least using a similar name so it is clear what the variable is being used for.
The most common tags used in conditional statements are <#if>, <#elseif>, and <#else>, although other logical operators like AND, OR, and NOT can also be handled through Freemarker.
If tags (<#if>)
are handled like standard HTML tags in that each opening if tag requires a matching closing if tag (</#if>). You will need a separate if tag for each different variable you are checking, and variables checks can be nested within one another.
Else-if tags (<#elseif>)
are used when checking multiple specific values for the same variable, such as the previous example where we checked for Male vs Female for gender. Else-if tags are a part of the If statement that first used that variable and do not require separate closing tags.
Else tags (<#else>)
are used to account for all possible variable values that are not specifically being checked for, including null values. Else tags are also part of the parent If statement and do not require separate closing tags.
Assigning Variables
Use if you are not sure if every recipient will have data in the fields that are referenced on their Lead/Contact records in the FreeMarker, variables are created. You must declare a variable. This allows the ability to put in some sort of value for a "null" case, this way people who may not have that field can be accounted for.
If you were to use an "Assign" statement to create a variable, such as "region", for ${Recipient.contact.address1_country[0]!""}, the IF statement would look like this:
<#if region == "United States">Then put content for United States here</#if>
Place the content you want in between the conditional statements.
NOTE: The content on the record in CRM must be exact, it is case-sensitive!
NOTE: If-statements can extend beyond the sections with ClickDimensions email template designers (for example, you can begin an if-statement in one block, and finish it in another).
What Will Not Work
Here is an example of what you cannot do (the conditional statement below will not work!):
NOTE: there is no <#assign> step, and the conditional statement is being run directly on the interpolation instead of on the variable:
<#if Recipient.contact.gendercode=="Male">
This content will display for men.
<#elseif Recipient.contact.gendercode=="Female">
This content will display for women.
<#else>
This content will display for recipients of unknown gender.
</#if>
Using Multiple Variables
As mentioned earlier, it is ok to use multiple variables within a single conditional statement as long as each utilizes a separate if tag. For example, if we want content to display based on both language and gender:
<#assign language=Recipient.contact.new_language[0]!"null"/>
<#assign gender=Recipient.contact.gendercode[0]!"null"/>
<#if language=="Spanish">
<#if gender=="Male">
This content will display for Spanish-speaking men.
<#elseif gender=="Female">
This content will display for Spanish-speaking women.
<#elseif gender=="null">
This content will display for Spanish speakers of unknown gender.
</#if>
<#elseif language=="English">
<#if gender=="Male">
This content will display for English-speaking men.
<#elseif gender=="Female">
This content will display for English-speaking women.
<#elseif gender=="null">
This content will display for English speakers of unknown gender.
</#if>
</#if>
Elseif Statements
Use "elseif" after the initial if-statement when you are checking different values for the same variable and shouldn't be used to check different variables.
<#if region == "North America">Then put content for United States here
<#elseif region == "South America">Then put content for South America here
</#if>
Else-Statements
Use an else-statement to display data that should show IF the recipient has any value for the variable EXCEPT what you have already checked for. For example, if you are showing specific material for recipients who live in North America, but other material for all other recipients outside of North America (any other region in the region field of the Lead/Contact/Account record besides "North America"), everyone else will see whatever you have marked in the else-statement.
Everyone with a "North America" value in the region field will only see material designated via FreeMarker code for "North America".
In this example it means "Anyone ELSE who does not have 'United States' in the Country Address 1 field on their Contact record. The <#else> statement should be placed immediately before the closing </#if> statement:
<#if region == "United States">
Then put content for recipients in the United States here
<#else>Then put content for everyone else here
</#if>
Feature Added: Original |
Feature Updated: 6.7.0 |
ClickDimensions Version Needed: 6.7.0 |