How Liquid Works

Liquid has three primary “pieces”—keys, filters, and tags—and these components work together to determine what dynamic content is displayed and how it looks. 

The actual Liquid code is kind of like a placeholder—it determines what data (aka, values) to pull in and “fill in the blank” with the output you want when the email is delivered to the customer. 

Here’s a snapshot of the three components of Liquid and how they interact with each other: 

You can use a key to reference a value, like this:

Hi {{ name }},
Hi {{ name }},

The output is the value that the key points to; it looks like this:

Hi Lee,
Hi lee,

You can use filters to modify the output, such as making sure the name is capitalized:

Hi {{ name | capitalize }},

Here’s the output now:

Hi Lee,

Finally, you can use tags to insert programming logic. For example, you could use a personalized greeting when you know the customer’s name and a generic greeting when you don’t, like this: 

{% if name != blank %}
Hi {{name}}!
{% else %}
Hi friend!
{% endif %}

If your data source has a value for the customer’s name, the output is:

Hi Lee!

But if you don’t have that customer’s name, here’s what you get:

Hi friend!

What’s extra cool about the example tag above is that it includes a “fallback statement”—some 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 put !=blank in the code, which tells Liquid what to do if the value of the name key is blank.

Using a fallback statement in a tag like the one above is a good habit to get into; otherwise, you can get some strange output. You’ll learn more about this in the intermediate module, so just keep this best practice in mind for later. 

Personalizing an email salutation is a simple example of what Liquid can do, but you can accomplish some pretty complex things with these basic concepts. You can use a key all by itself, or you can add a filter and/or a tag to affect the key—they all work together. Hooray for teamwork!