Do you want to create an email that displays entirely different content based on some attribute from the recipients’ contact records? You can do that with FreeMarker! In addition to being used for populating dynamic content, such as a recipient’s name, into an email, FreeMarker can be used for exerting control over what the recipient receives by using conditional statements. Conditional statements generally take the form of if/else logic and allow you to show entirely different content to different recipients based on some attribute listed on their lead or contact record.

In this post, we will look at an example that will allow you to show different content to your email recipients depending upon the language and city listed on their record. The example will also incorporate logic to account for scenarios where the language or city fields on the recipient’s record may be empty.

This is the logical structure of the example template we will be building:

If the recipient speaks English…

If the recipient is in Atlanta…

Content for English-speaking recipients in Atlanta will be displayed.

If the recipient is in Miami…

Content for English-speaking recipients in Miami will be displayed.

If the recipient’s city field is empty…

Content English-speaking recipients whose city is unknown will be displayed.

If the recipient’s city field contains any value other than Atlanta or Miami…

Non-city-specific content for English speakers will be displayed.

If the recipient speaks Spanish…

If the recipient is in Atlanta…

Content for Spanish-speaking recipients in Atlanta will be displayed.

If the recipient is in Miami…

Content for Spanish-speaking recipients in Miami will be displayed.

If the recipient’s city field is empty…

Content for Spanish-speaking recipients whose city is unknown will be displayed.

If the recipient’s city field contains any value other than Atlanta or Miami…

Non-city-specific content for Spanish speakers will be displayed

Content for all recipients will be displayed.

Assigning Variables in FreeMarker

To begin, we will need to create variables to help account for any data that may be missing from the recipient’s record. If a field doesn’t contain data, FreeMarker will not have a value to compare in the conditional statement and it will produce a render error. Using variables allows us to incorporate placeholder text to prevent render errors from occurring. In our example, we are not sure if everyone will have data in the city or language fields. So, we need to create two variables: “city” and “language.”

We will start with a FreeMarker assign statement. This will allow us to define variables that we can use in the place of the actual FreeMarker dynamic content. For example, we will be creating the variable “language,” which will be used in place of the full FreeMarker text: ${[0]!””}.

Start with an opening assign statement <#assign language= then use the personalization dropdowns within the Email Template editor to insert the fields you want to check.

In our example, we now have:

<#assign language=${[0]!””}

Next, we will need to make a few adjustments to the default FreeMarker syntax. First, remove the ${ and the closing } from the language field FreeMarker. Then we will add placeholder text to account for scenarios where the language field may be empty on the recipient’s record. The !”” part of the FreeMarker is used to define placeholder text; we will choose the word “null” as our value. Finally add a backslash (/) and a greater than sign (>) to close out the assign statement.


Now, when we use this variable, it will set “language” equal to the value in the Language field for this particular contact, but if there is no value in the Language field, the variable will be set to “null.” The placeholder text ensures that even if the language field on the recipient’s record is empty, we will still have a value to check in the conditional statements.

Repeat the process to set up a second variable for city.

Set up Conditional Statements to Determine What Data the Recipient Sees

Now we will set up a series of conditional statements that will determine which content the recipient sees. Begin by adding the first if statement as <#if language==”English”>, then add a second statement for the city: <#if city==”Atlanta”>. This nests the second if statement within the first one, so a recipient will need to meet both criteria to see the relevant content in the email.

NOTE: When we assign a variable, one = sign is used, but conditional statements use two = signs.

We now have this:

<#assign[0]!”null”/><#assign[0]!”null”/><#if language==”English”><#if city==”Atlanta”>

Now add the content that an English-speaking customer in Atlanta would see. After that, we check if the recipient is in Miami or if the city field is blank. Since we are checking for a different value for previously used and established variable, we will use #elseif instead of #if.

<#elseif city==”Miami”>, and <#elseif city==”null”>

To account for any value of city except the values we already checked, we can use <#else>. If the recipient’s city is listed as anything other than Atlanta, Miami or null, they will see the content after the <#else> tag.

When the email is sent, the recipient will only see the content that exists after the if statement that is applicable to them and before the following if statement that checks for the next set of conditions. An English-speaking resident of Atlanta would see the content tailored to them, but none of the Miami content or the Spanish content would be displayed since the recipient did not meet the necessary criteria for those conditional statements.

Now that we have added conditional statements to account for the different cities in which our English-speaking recipients live, we need to close out the logic for checking the city, but not for checking the language. We want to do this because we now need to basically recreate all the content we just made, but for Spanish-speaking recipients. Use a closing if tag, </#if> to end the city conditional logic.

After all the Spanish language content, we will end with two </#if> tags; one to end the second conditional statement that checks the recipient’s city and a second to end the conditional statement that checked the recipient’s language.

Here’s the final template:

…and this is the resulting email when the template is sent to an English-speaking recipient in Atlanta:

With the addition of conditional statements to our template, we can now send out one email and dynamically cater the content to a several different audiences.

Always Test Your Work

When working with FreeMarker, be sure to always test your email before sending it out to your recipients. An easy way to do this is to add yourself to the email send as a recipient and send it to yourself first to make sure it works correctly. If you do not receive the email, there was probably a render error due to incorrectly formatted FreeMarker. You can see the render error by going to the Email Events in the email send’s Related Records menu.

You can see more details by opening the render error email event and look at the message to see what is wrong.

If you get a render error, you can fix the listed error in your email template and send the email to yourself again. If you are unsure how to fix it or do not understand the error message, feel free to reach out to us for assistance!

Note: This is an update to an older post found here.

The post Advanced Dynamic Content – Conditional Statements appeared first on ClickDimensions Blog.