Introduction to Liquid Tags and Dynamic Content in Emails

Avatar

By Jitta Rao

updated about 1 month ago

Include a Liquid Tag in an email.

Gist allows you to include a message, that will only be seen if the Contact has a specific property in their profile.


Basic contact properties

If you want to include a dynamic contact attribute, you'll want to include the tag that references that attribute. For example, if someone has an attribute on their profile called first_name, the tag would be:

{{ contact.first_name }}

As you can see, anything you've included as a contact attribute should start with contact. before your attribute name. If you've got country, for example, it'd be {{ contact.country }}

Filters and tags

Filters and tags make up the foundations of Liquid, and there are a number of them that you can use. In the first-name example, if you wanted to make sure it was capitalized, add a capitalize filter to the tag:

{{ contact.first_name | capitalize }}

If the field was full_name and you wanted to include only the first word/name, you could use:

{{ contact.full_name | split: " " | first }}

Fallbacks

If only a subset of your contacts have the specific attribute you want to include, you'll want to only use that tag for matching contacts and set up a fallback otherwise.

{% if contact.first_name != blank %}
  {{ contact.first_name | capitalize }}
{% else %}
  there
{% endif %}

Links

You can include properties within links as well, in order to send your contacts to a custom page.

Example:

<a href="https://www.yoursite.com/{{contact.PROPERTY_NAME}}">

Displaying a timestamp as a regular date

Let's say you have a contact attribute called expiration (for a trial, maybe) that you store as a timestamp. If you wanted to display that expiration date in messages as a human-readable date, you can do so with liquid. There are a few different date filters you can use with this, but here we're just going to show you month name, day, and year.

Example

If you had a contact attribute called expiration with a timestamp value of 1596153600, you can do the following:

{{contact.expiration| date: "%B %-d, %Y"}}

The result in your message would be July 31, 2020.

The spaces, commas, etc between the date filters are included in the output. So if you want 31/07/2020 you'd do %m/%d/%Y.

For the current date, you can use:

{{ 'now' | date: "%B %-d, %Y" }}

Displaying how many days left in a trial

{% assign current_date = 'now' | date: '%s' %}
{% assign future_date = contact.trial_end %}
{{ future_date | minus: current_date | divided_by: 86400 }}

Explanation

  1. You can get the current epoch time with 'now' | date: "%s". %s is a formatting option.
  2. The contact.trial_end date is in epoch time.
  3. Since epoch time is in seconds, we're dividing by 86400 (number of seconds in a day) to get the number of days. Keep in mind that this is integer division so it'll be rounded down.

Show a different message based on day of week

{% assign day = 'now' | date: '%A' %}
{% if day == 'Friday' %}
Have a great weekend!
{% else %}
Cheers,
{% endif %}

Looping through properties

For this example, imagine each person has in their profile a list of friends, and you want to include this list in a message. Here's an example of what the data you send to Gist might look like in Javascript:

gist.identify({
  id:               "1",
  friends:   [ "Elaine", "George", "Kramer"]
});

Say the contact with an id of 1 is "Jerry". When you're composing messages for Jerry, you can display his friend list using a Liquid for loop:

{% for person in contact.friends %}
  {{ person }}<br/>
{% endfor %}

And that's it! That'll display:

Elaine George Kramer

Conditional content

{% if subscriber.tags contains 'tag1' %}

This text will be sent to Contacts with the "tag1' tag on their profile.

{% endif %}


As seen inside Gist Email Composer: (Color added to show different components of Liquid Tag)


Add multiple variables to your liquid tag.

Gist allows you add more than one variable to your liquid tag. Now you can send 1 email, and Contacts will receive different messages depending on which property they have or don't have.


Example:

Email message example text is bolded.


{% if subscriber.tags contains "tag1" %}

This text will be sent to Contacts with the "tag1' tag on their profile.

{% elsif subscriber.tags contains  "tag2" %}

This text will be sent to Contacts with the "tag2' tag on their profile.

{% else %}

This text will be sent to Contacts that do not have "tag1" or tag2" on their profile.

{% endif %}


As seen inside Gist Email Composer: (Color added to show different components of Liquid Tag)


These are the operators that can be used:

Note: The following would replace "contains" in the above examples.

{% if subscriber.property contains "tag1" %}

{% if subscriber.tags contains "tag1" %}

{% if subscriber.web_sessions <= "100" %}


`==`  is equal to

`!=` is not equal to

`<` is less than

`<=` is less than or equal to

`>` is greater than

`>=` is greater than or equal to

`and`checks that both condition A and condition B exist

`or` checks that either condition A or condition B exist

`contains` checks for a substring within a string or an array


Using "Unless" Liquid Tags

There are certain situations where you won't want certain subscribers seeing certain messages. For these use cases, we've included a condition to keep certain properties from seeing a message.

Here is an example:

{% unless subscriber.tags contains 'zapier' %}
Welcome
{% endunless %}

So, essentially, you're replacing the word "if" for "unless" which will cause subscribers tagged with that property to not see the specific message within a liquid tag.

Need Help?

If you have any further questions, please start a Live Chat. Just "Click" on the Chat Icon in the lower right corner to talk with our support team.

Did this answer your question?