Conditional Content
Conditional content (also known as Dynamic Content), allows you to alter the email content of your campaigns based on a condition that affects or uses the information you have stored about your contacts. This allows you to tailor your campaigns for each recipient—as certain parts can be displayed or hidden based on the conditions matched—providing the opportunity for greater engagement and higher conversions.
Note: This documentation refers to coding Conditional Content. Refer to the Email Editor topic for help on using the Conditional Content UI.
On this page:
You can create conditional content in the Source Editor, HTML Editor or the Email Editor. There is further conditional content information found in the Template Directives language topic, as well as additional Template API information.
You can also perform simple personalisation in your campaigns.
Formatting Conditional Content
When writing conditional content the code is written between brackets and asterisks [* .... *]; everything contained within them is part of the template tag.
data is used as the function call, this takes a single argument contained in brackets [* data( .... )*] (this will be your table field) and will return the value for the contact.
The field name must be exactly as it appears in the table and placed within single quotes. It is also required that data table fields are prefixed with the table ID, separated with a full stop e.g. '2.address', however core table fields do not need the ID prefix.
Note: If you have table fields that have identical names, then you must specify the table ID.
Filters
Filters are separated by a pipe, they will affect the data in some way; |html, for example, encodes data so it is safe to use in HTML. We recommend using this to prevent HTML control characters from affecting the layout.
You may be familiar with other filters from using the Personalisation tool in the HTML Editor, which include:
- |upper: displays the output value in uppercase
- |lower: displays the output value in lowercase
- |ucfirst: will capitalise first letter
- |trim: automatically remove any excess white space contained within the field
Filters can be used in combination with each other and a common example of this would be for firstname and surname personalisation—to lowercase the whole value and uppercase the first letter only. The filters are applied to the data in order, so for this example |lower would precede |ucfirst and would display the personalisation as "Dear John", for example.
Dear [* data('first_name') |lower |ucfirst |html *]
Default Value
Default values can be used if there is no value in the specified field. The text for the default value needs to be separated with a double pipe, contained within single quotes and placed before any filters.
This example might display "Dear FirstName" or "Dear Customer", depending on the data:
Dear [* data('first_name') || 'Customer' *]
It is also possible to use your data to personalise your subject lines, as part of the campaign set-up. For help adding personalisation to your subject lines, refer to the Campaign Options topic.
You can always apply more complicated logic using different combinations of template tags.
IF
IF allows you to create basic conditions; if the condition is true, then the subsequent content will be included in that recipient's copy of the campaign. If the condition is false, then it won't be included.
You can set the condition so that as long as the field contains a value then the content is displayed for that contact.
In the example below, if the contact has a value in the 'country' field, then the image will be displayed.
[* IF data('country') *]
<img src="http://example.com/hero_image">[* END *]
Or, you can be more specific so the field must contain and match a specified value.
In this example, the contact must have the value 'England' in the 'country' field for the image to be displayed.
[* IF data('country') == 'UK' *]
<img src="http://example.com/hero_images/UK">[* END *]
The template tag [* END *] indicates the end of the condition block. Treating this example like a building block, it can be built on to create more complex content.
IF is most commonly used for situations where name information is missing in the data; below are a number of examples using ELSE and ELSIF conditions.
ELSE
ELSE indicates an alternative to the IF condition.
In the example below, if the contact has a value in the 'first_name' field the information to update details would be displayed. If there were no value in the field, the alternative text would be displayed.
[* IF data('salutation') *]
Dear [* data('salutation') |ucfirst | html *]
[* ELSE *]
Dear Colleague
[* END *]
ELSIF
ELSIF allows you to add alternatives to the IF condition.
[* IF data('salutation') *]
Dear [* data('salutation') |ucfirst |html *]
[* ELSIF data('first_name') *]
Dear [* data('first_name') |ucfirst |html *]
[* ELSE *]
Dear Colleague
[* END *]
Although this example may look complicated, when broken down you can see this is simply built up with a series of template tags. They are evaluated in order: if the IF condition isn't true, the ELSIF and ELSE conditions will be looked at.
In this case, if the contact has a value in the 'salutation' field the subsequent text will be displayed:
[* IF data('salutation') *]
The next template tag contains the ELSIF condition, and would be evaluated if the IF condition was false. In this example, if the contact has a value in the 'first_name' field, the subsequent text would be displayed:
[* ELSIF data('first_name') *]
If you are using multiple ELSIF conditions, they will be looked at in order until one is true.
If none of them are true the ELSE condition would come into effect and the default would be displayed:
[* ELSE *]
Dear Colleague
If there were no ELSE condition, and none of the ELSIF were true, then the condition would simply have no effect.
Returning to the series of template tags in the original example, if the contact has a value in the 'saluation' field, this data would be inserted. If not, data from the 'first_name' field would be inserted. If there were no value in that field either, the alternative text "Dear Colleague" would be displayed.
AND/OR
You can apply more complicated logic by extending the template tag to include multiple fields, on an AND or OR basis.
Including AND within the template tag means that all of the conditions need to be true, whereas including OR means that only one condition must be true.
This example would require values in both the 'title' and 'surname' fields, otherwise the alternative text would be displayed:
[* IF data('title') AND data('surname') *]
Dear [* data('title') |html *] [* data('surname') |html *]
[* ELSE *]
Dear Colleague
[* END *]
This example would only require a value in one of the fields for the subsequent text to be displayed:
[* IF (data('purchase_order') OR data('customer_number')) *]
Update your details here.
[* END *]
You can also use AND and OR in combination; this example would require data in both the student and subscription fields, or just the graduate field, for the text to be displayed:
[* IF (data('student') AND data('subscription')) OR data('graduate') *]
Click here for your free download.
[* END *]
This can also be extended to include specific values:
[* IF ((data('country') == 'UK' AND data('member') == 'gold') OR (data('country') == 'Ireland' AND data('member') == 'gold')) *]
Click here to subscribe.
[* END *]
Negating Conditions
To negate a condition, prefix it with an exclamation mark !.
For example, the following condition will display the image if the contact does not come from the UK:
[* IF !data('country') == 'UK' *]
<img src="http://example.com/hero_images/Globe">[* END *]