FreeMarker: Conditional Statements

  << Personalization Menu FreeMarker Restrictions >>

About Conditional Statements

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.

Important: When setting up a conditional statement you must begin by assigning a variable using the #assign Freemarker Tag, and then base the conditional statements on the value of this variable.

Example:

<#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 unknown gender.
</#if>

The standard structure for a conditional statement is

  1. assign variables
  2. check conditions
  3. else scenario
  4. end statement

First, any Freemarker that you want to use in the conditional statement should be declared as variables, then 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. 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 (examples of how to use these operators can be found on our blog).

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 delcare 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’re using an “Assign” step to create a variable for ${Recipient.contact.address1_country[0]!””} such as “region”, 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).

Example FreeMarker Template

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==”1″>
This content will display for men.
<#elseif Recipient.contact.gendercode==”2″>
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 men.
<#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 Need: 6.7.0 
 << Personalization Menu FreeMarker Restrictions >>