Skip to main content

An introduction to the Liquid template language

According to the State of Messaging Report 2023, 81% of brands find personalization important to their messaging strategy, and 73% say they plan on increasing their use of personalization tactics in 2024. 

With more ways to connect with customers than ever, it’s no surprise that brands are turning to personalization tactics to drive engagement and help them stand out from the crowd. That’s where the Liquid template language can make all the difference.

Liquid is a powerful addition to every marketer’s toolkit. With it, you can transform the data you have about your customers into content that lines up precisely with who they are and what they do. Bonus? Whether or not you have a background in coding, it’s easy enough to become a Liquid pro, and this overview will help you get started.

Here’s what we’ll be covering today:

What is Liquid?

The primary purpose of the Liquid template language, often referred to simply as Liquid, is to insert dynamic content into static templates. Liquid has three primary components: keys, filters, and tags. They work together to determine what dynamic content is displayed and how it looks upon receipt (more on that below).

Being an open-source language, anyone can modify Liquid and make new versions—and many folks have! Today, it’s widely used for web and email marketing, and many people have molded Liquid to meet their needs.

The three main components of Liquid

Keys, filters, and tags each play a distinct role in the Liquid templating language, and you’ll need to understand all three if you want to leverage the full potential of Liquid. Let’s jump into an overview of each component.

What are Liquid keys?

At their simplest, keys, also known as objects, are containers for dynamic data enclosed in double curly braces, like this: {{ key goes here }}. For example, if customer is the key and the value it refers to is a customer’s name, it would be written like this:

Hi {{customer.first_name}},

If the customer’s name were lee, it would render like this:

Hi lee,

Keys can also contain a collection of other keys. For example, the key may be customer but within that key, you might nest options for first_name and last_name. To include both names in your messaging, you’d then need to reference the primary key, customer, and both the first_name and last_name keys using dot notation to separate each one:

Hi {{customer.first_name}}{{ customer.last_name}},

Pro tips:

  • Keys can only contain letters and numbers. To avoid unnecessary added complexity, we strongly recommend using an underscore instead of a space when creating key names, like this: first_name. If you include spaces in key names, you must enclose the key in square brackets every time you reference it, or your code will break. Here’s how that looks: {{ customer["first name"] }}
  • An understanding of JSON is required for a key to reference a collection of keys. We cover that in-depth in our guide to intermediate and advanced Liquid.
  • Putting a space on either side of the key within its curly braces is common; it makes your code easier to read—but you can leave the spaces out without breaking anything.

What are Liquid filters?

Filters are used to modify the output of Liquid. To give you a few examples, you might use filters to:

  • Capitalize a word (like a person’s name)
  • Calculate the end date of a free trial 
  • Convert a timestamp to a date that’s easy on the eyes

Filters always go after the key inside the double curly braces. For example, if you’d like to capitalize the person’s name, simply write capitalize after a pipe character (|) within a key’s curly braces. It would look like this:

Hi {{customer.first_name | capitalize}},

Keeping the same value of lee as the example, the output now would end up like this:

Hi Lee,

Pro tip: You can use as many filters as you like for each key. Just remember: Liquid will apply the filters from left to right. For example:

{{ key.key | filter | filter }

Sometimes, the order in which you list filters makes no difference—like when you apply the capitalize and strip filters to the same key to capitalize a name and eliminate whitespace. But other times, filters do more complex manipulations, like math calculations. In that situation, filter order matters a lot.

What are Liquid tags?

Tags allow you to level up your Liquid output with a layer of logic. They’re always enclosed in curly braces with percentage signs: {% tag goes here %}. In general, tags use true-or-false (aka, Boolean) logic to determine the output to display. They tell Liquid that if a specific condition is true, then Liquid should display a particular output. If the condition is false, Liquid should move on. 

Tags can include conditional and logical operators, influencing how and what content is displayed in messaging. Here’s an overview of a few common operators you might use:

==equals
!=does not equal
>greater than
<less than
>=greater than or equal to
<=less than or equal to
orlogical or
andlogical and

For example, you can use tags to add conditional statements, like creating a personalized greeting when you know the customer’s name and a generic greeting when you don’t:

{% if customer.first_name != blank %}
Hi {{customer.first_name}},
{% else %}
Hi friend,
{% endif %}

If your data source has a value for the customer name, like Lee, the output is:

Hi Lee,

But if you don’t have that customer’s name, you can specify what the output should be in lieu:

Hi friend,

Pro tip: 

With tags, like in the example above, you can include a “fallback statement,” which is simply a bit of code that tells Liquid what to do if the value of a key is empty (e.g., you don’t have any data for the value the key is trying to reference). There are various fallback statements you can use depending on what kind of value you’re dealing with; in this example, we’ve used the operator !=blank in the code to tell Liquid what to do if the value of the name key is blank.

Real-life scenario: keys, tags, and filters in action

Now let’s apply all three components of Liquid to a real-life scenario: a customer who’s just ordered a dog leash from your online store, Tip Top Pets. 

Let’s pretend you’re preparing a confirmation email for whenever a customer makes a purchase, which will have both static (always the same) and dynamic (personalized for each email) content. 

Since you sell cat and dog products, you want to say one thing if your customer owns a dog but something different if they own a cat. And if you don’t know what kind of pet they have, you need a default third option that covers all your bases (who knows, maybe they bought that leash for an unruly sea urchin!). 

You have the following keys about your client. In this example, each key contains several other keys:

Liquid template language: key values about a client

So, you come up with three sentences, one for each possible scenario. In this case, Spot is a dog, so we’ll start the body copy with: “Ruff ruff! We’ve received your order!” We know Spot is a dog because dog is the value of the customer.pet.pet_type key.

Let’s see how the key, filter, and tag work together to dynamically select the right content:

{% if customer.pet.pet_type == "dog" %}
Hi {{ customer.first_name | capitalize }},
Ruff ruff! We’ve received your order! Hope {{ customer.pet.pet_name }} loves your purchase!
{% elsif customer.pet.pet_type == "cat" %}
Hi {{ customer.first_name | capitalize }},
Meow! We’ve received your order! Hope {{ customer.pet.pet_name }} loves your purchase!
{% else %}
Hooray! We’ve received your order! Hope your pet loves your purchase!
{% endif %}

The various outputs would then look like this:

Hi Lee,
Ruff ruff! We’ve received your order! Hope Spot loves your purchase!
Hi Lee,
Meow! We’ve received your order! Hope Luna loves your purchase!
Hooray! We’ve received your order! Hope your pet loves your purchase!

Liquid: a powerful tool for personalization

There’s no doubt that the Liquid template language can empower you to seamlessly transform your customer data into powerful, personalized messages. With keys, filters, and tags, you can create content that reaches your customers at the right time with the right message.

But this is only the beginning. 

With intermediate and advanced Liquid techniques, you can leverage complex data structures in your messaging, implement conditional statements with finesse, and orchestrate the most nuanced content campaigns.

Ready to take up a notch? Grab our two-part guide to intermediate and advanced Liquid.