Using tags to put logic into your campaigns, templates and landing pages

Summary

Tags are used when building logic to tell your content what to do, for example:

{% if contact.data.firstname == 'ben' %}
Hey Ben!
{% else %}
Hello there!
{% endif %}

In the above example, this will show different content depending on whether or not the contact's first name is 'ben'. This functions pretty much as our regular dynamic content.

A more interesting example is using "for" loops, for example:

{% for book in contact.addressbooks %}
{{ book.name }} <br/>
{% endfor %}

This will get the list of address books that a contact is in, and for each address book show the name of the book followed by a line break.

Supported tags

The following tags are supported in our platform:

Assign

Assign is used to store data as a temporary variable for output (or logic) elsewhere in the campaign, template or landing page.

For example:

{% assign fullname = contact.data.firstname | append: ' ' | append: contact.data.lastname %}
{{ fullname }}

Outputs: Beatrix Potter

Capture

Capture, like assign, stores data as a temporary variable. It is typically used for longer or more complicated variables and captures all content in the block between a {% capture %} tag and an {% endcapture %} tag. For example:

{% capture fullname %}{{contact.data.firstname}} {{contact.data.lastname}}{% endcapture %}
{{ fullname }}

Outputs: Beatrix Potter

Comment

The contents of a comment block will not be included when your campaign, template or landing page is rendered. It is a way for you to make a note in your markup. A comment block begins with a {% comment %} tag and ends with an {% endcomment %} tag. For example:

{% comment %}
This bit of markup does something clever to assign the right gender to the contact
{% endcomment %}

Outputs: nothing

Raw

The contents of a raw block will not be evaluated as liquid markup when your campaign, template or landing page is rendered. You can use this to include raw liquid markup in the interface, should you wish to. A raw block begins with a {% raw %} tag and ends with an {% endraw %} tag. For example:

My {% raw %}{{ contact.data.fullname }}{% endraw %} is {{ contact.data.fullname }}.

Outputs: My {{ contact.data.fullname }} is Michael Caine.

If (else and elseif)

If statements provide basic conditional logic as to what content should be shown or not shown. An if statement contains a condition, and if that condition is met then the contents of the if block are included in the campaign, template or landing page. If statements begin with an {% if %} tag and end with an {% endif %} tag.

For example:

Hi there! {% if contact.data.firstname == 'Barney' %}You're looking particularly awesome. {% endif %}Isn't it a lovely day? Would you like a piece of cake?

Outputs (for Barney): Hi there! You're looking particularly awesome. Isn't it a lovely day? Would you like a piece of cake?
Outputs (for everyone else): Hi there! Would you like a piece of cake?

Elseif statements can be used inside an if block to add further conditional content if the first condition is not met. For example:

Hi there! {% if contact.data.firstname == 'Barney' %}You're looking particularly awesome. {% elseif contact.data.gende r== 'Male' %}Isn't it a lovely day? {% endif %}Would you like a piece of cake?

Outputs (for Barney): Hi there! You're looking particularly awesome. Would you like a piece of cake?
Outputs (for all other males): Hi there! Isn't it a lovely day? Would you like a piece of cake?
Outputs (for everyone else): Hi there! Would you like a piece of cake?

Else statements can be used inside an if block to add default conditions if the none of the preceding conditions are met. For example:

Hi there! {% if contact.data.firstname == 'Barney' %}You're looking particularly awesome. {% elseif contact.data.gender == 'Male' %}Isn't it a lovely day? {% else %}Would you like a piece of cake?{% endif %}

Outputs (for Barney): Hi there! You're looking particularly awesome.
Outputs (for all other males): Hi there! Isn't it a lovely day?
Outputs (for everyone else): Hi there! Would you like a piece of cake?

In these examples "==" is used to test equivalence, below is a full list of available operators.

Unless

The unless block is the reverse of the if block, it shows its content if the condition evaluates to false. It begins with an {% unless %} tag and ends with an {% endunless %} tag.

For example:

Yo, {{ contact.data.firstname }}!{% unless contact.data.firstname == 'Ted' %} Haaaaave you met Ted?{% endunless %}

Outputs (for Ted): Yo, Ted!
Outputs (for everyone else): Yo, Tracy! Haaaaave you met Ted?

Case

A case block is used in place of a series of if statements, where different content is to be shown based on the value of a single field. A case block starts with a {% case %} tag, ends with an {% endcase %} tag, uses {% when %} tags for each condition, and an {% else tag %} to denote the default content. For example:

{% case contact.data.firstname %}
{% when 'Simon' %}
A lager, sir?
{% when 'Tink' %}
Lager top for the gentleman?
{% when 'Simone' %}
Can I get you a glass of red?
{% else %}
I ain't getting you a drink!
{% endcase %}

Outputs an offer of a drink for Simon, Tink and Simone; but not for anyone else. 

Iteration logic

The following tags specifically help with iteration logic.

For

A "for" block will repeatedly iterate through its contents for a number of iterations.

For example:

{% for book in contact.addressbooks %}
{{ book.name }}<br/>
{% endfor %}

This will list all address books a contact is in, each separated by a line break.

Note that the syntax for a for loop involves a new object, in this case "book", that represents each member of the contact.addressbooks array that we iterate through. Rather than referring to "contact.addressbooks[n].name"; we now refer to "book.name".

By using a sort filter, we can specify the order that the iterations occur in; for example:

{% assign mybooks = contact.addressbooks | sort: 'name' %}
{% for book in mybooks %}
{{ book.name }}<br>
{% endfor %}

This first sorts the address books that a contact is a member of, sorts them by name, and then lists them (with a line break between each address book).

Reverse order

You can reverse the way a for loop iterates by using the "revered" parameter as follows:

{% assign mybooks = contact.addressbooks | sort: 'name' %}
{% for book in mybooks reversed %}
{{ book.name }}<br>
{% endfor %}

This lists the address books in reverse alphabetical order.

Limited iterations

You can limit the number of times a for loop will iterate by using the "limit" parameter as follows:

{% assign mybooks = contact.addressbooks | sort: 'name' %}
{% for book in mybooks limit: 3 %}
{{ book.name }}<br>
{% endfor %}

This will list the first three address books alphabetically that a contact is a member of.

Offset iterations

You can decide to not start at the first member the array, the "offset" parameter allows you to skip a number of records. For example

{% assign mybooks = contact.addressbooks | sort: 'name' %}
{% for book in mybooks limit: 3 offset: 2 %}
{{ book.name }}<br>
{% endfor %}

Lists the third, fourth and fifth address books, alphabetically, that the contact is a member of.

Ranged iterations

If you want to iterate for a set number of times through a piece of text/markup, but it is not tied to a particular array you can do this using a range. For example to iterate through a piece of code five times you can use this:

{% assign stars = 5 %}
{% for i in (1..stars) %}
&#8902;
{% endfor %}

Will show five stars (&#8902; is code for a star).

Your ranged iteration does not need to start at "1" for example:

{% for value in (68..72) %}
{{ value }}
{% endfor %}

Will output the numbers 68 69 70 72.

Break

The break tag causes a loop to stop iterating, even if it has not reached the final iteration. For example:

{% for i in (1..10) %}
{% assign remainder = i | modulo:7 %}
{% if remainder == 0 %} {% break %} {% endif %}
{% i %}
{% endfor %}

Will output the numbers 1 2 3 4 5 6 but break out of the loop before outputting 7.

Continue

The continue tag causes a loop to jump to the next iteration without  completing the current one. For example:

{% for i in (1..10) %}
{% assign remainder = i | modulo:3 %}
{% if remainder == 0 %} {% continue %} {% endif %}
{% i %}
{% endfor %}

Will output the numbers 1 2 4 5 7 8 10; but skipping the numbers that are divisible by 3.

Cycle

Cycle tags are used within for loops. The parameter for a cycle tag is a list of options; each time the for loop iterates it will take the next option in the list, returning to the first. For example:

{% for i in (1..20) %}
<p style="color:{% cycle '#FF0000', '#00FF00', '#0000FF' %};">
{{i}}
</p>
{% endfor %}

This will list the numbers 1 to 20 alternating the colours red, green and blue starting with red (#FF0000) for number 1 and ending with green (#00FF00) for number 20.

Forloop

Forloop is not a tag, but an object that is referencable from within a for block. The forloop can be interrogated to find out where in the series of iterations we currently are. For example:

{% for i in (1..10) %}
{% if forloop.last %} and {% endif %}{{ i }}{% unless forloop.last %}, {% endunless %}
{% endfor %}

Will list a comma separated list of numbers with an "and" before the last number: 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10

The following elements of the forloop can be referenced:

forloop.first
Returns 'true' if this is the first iteration through the loop; otherwise it returns 'false'.
forloop.index
Returns the current index of the loop. i.e. the number of iterations that have been started, starting at 1 for the first iteration.
forloop.index0
Returns the current index of the loop starting at zero. i.e. the number of iterations that have ended.
forloop.last
Returns 'true' if this is the last iteration through the loop; otherwise it returns 'false'.
forloop.rindex
Returns the forloop index starting in reverse order (so 1 for the last iteration, 2 for the penultimate iteration and so on).
forloop.rindex0
Returns the forloop index starting in reverse order starting at zero (so 0 for the last iteration, 1 for the penultimate iteration and so on).
forloop.length
Returns the total number of iterations the loop will go through.

Operators for logical statements

When using logical tags such as if, else and unless the following operators can be used used:

  • == → Is equal to
  • != → Is not equal to
  • > → Is greater than
  • < → Is less than
  • >= → Is greater than or equal to
  • <= → Is less than or equal to
  • contains → Contains

Conditions can be chained together using 'or' and 'and'.

Examples

{% if contact.data.firstname == 'ben' %}
Hey Ben
{% endif %}

Conditions can also be combined:

{% if contact.data.firstname == 'ben' and contact.data.age < 12 %}
Hey young Ben
{% endif %}

And then using the contains operator you could do:

{% if contact.data.firstname contains 'B' %}
You are truly awesome; your name contains a B
{% endif %}
Have more questions? Submit a request

Comments

  • Avatar

    Very interesting BTW would this be the way in a landing page to use a query string value to define which picture to display or is that possible through the dynamic content?

Powered by Zendesk