Home | Dynamic Content | Using FreeMarker to Generate Conditional Email Footers

Using FreeMarker to Generate Conditional Email Footers

If your company maintains multiple brands out of one Dynamics environment or services customers in multiple countries, you may find at times that you have email templates that could apply to all of your brands or areas of service. So, it just makes good sense to use the same template across the board. A common problem is that when you start setting up a flexible, reusable template, you may realize that your subscription management page is going to quickly become overcrowded and convoluted. Maybe you need to include unique terms of service depending on the country in which your customer resides. Or maybe you want to restrict certain customers from subscribing to multiple lists. So, what do you do?

Fortunately, using conditional FreeMarker, it’s easy to create a dynamic footer that serves up a different subscription management page to different recipients all in the same template. First, though, you will need to do a bit of setup and determine the criteria that is most valuable or relevant for your recipients. For this blog post, I’m going to be referencing the Address 1 Country field on the contact and lead entities as it’s a standard field, and I won’t have to add anything custom to the entity form to do my segmentation. If you’re filtering by customer type, you may need to add a custom field to indicate what type (i.e. business catering, in-store rewards or wholesale) a given contact or lead is. If you’re going that route, I would recommend doing so with an option set, such as with a drop-down menu, as it standardizes your choices and prevents misspellings or typos.

Next, make sure you have created all the subscription management pages that you want to link to in your email template’s footer. For this example, I have created three subscription management pages – one for German customers, another for French customers and another for everyone else – with appropriate terms of service included in each. You can read up on how to create subscription management pages here.

Now that our pages have been created, we need to identify how we’re determining which subscription page we want to link to for a given recipient. As I mentioned before, I’m using the Address 1 Country field, but if you’re segmenting by type, this is where you would identify the type that a given contact or lead falls under. Now that we have chosen our field, we can go to our template and do some setup for our FreeMarker. For your reference, this is what my full template looks like:

In this blog post, we will focus on the FreeMarker found here:

And here:

When writing conditional statements in FreeMarker, it is important to remember that we can’t directly reference an interpolation in a logical check.

So, this doesn’t work:

Instead, we need to first assign our values to variables so we can use them throughout the template. For this to work, we need to assign two variables, one for contacts and one for leads.

Let’s start by pulling in the interpolations we need to use for these values. Go to the personalization menu for the editor you’re using, and select the fields you want to reference on the contact and lead entities.

Now, our FreeMarker should look like this:

With those in place, we want to remove the “$” and “{}” and wrap these interpolations in <#assign/> statements that assign them to variables we can reference throughout. I’ve decided to name my variables contact_country and lead_country. These should look like this:

You’re now ready to set up your footer. I’m using a relatively simple text navigation footer that looks like this:

After adding my FreeMarker, it looks like this in the editor:

I added line breaks here for readability, but once we’re done, we will clean those up as well. Once you have your conditional statement laid out, insert the links to the appropriate subscription management pages. You can insert these URLs through the hyperlink manager by selecting the Unsubscribe tab and choosing the subscription management page you want to insert.

Breaking this down, we have created three links, one for each subscription management page, and then nested them in an If Statement that checks to see if a recipient is from France or Germany and renders the correct subscription page based on that information. Then, if neither of those checks is true, it publishes the default <#else> statement instead.

Breaking this down line by line:

We have opened our If Statement and made our first conditional check in this <#if> tag. We’re checking to see if contact_country or lead_country is equal to “France”. If you aren’t familiar with it already, the double pipes ( || ) expression you see is a common way of saying “or” in many programming and templating languages. FreeMarker uses it this way as well.

We used the <#elseif> tag and the same syntax from the If Statement on the first line to check if someone is from Germany.

If they don’t match either of the earlier if statements, the recipient will see the default link listed after the <#else> tag.

We close out the If Statement with the closing </#if> tag to complete our FreeMarker template.

Now, we delete all the line breaks we created for readability so that whatever link renders, it renders on the same line as the rest of your footer. This will remove unwanted white space.

You now have a dynamic footer that displays different subscription management pages to customers in different countries!

Happy Marketing!

About the Author:

mm
Chris Fairfax is a ClickDimensions Product Consultant.

Leave A Comment