Intermediate tags

Just got here? This module is part of a full-length Liquid tutorial. Start from the beginning.  

In this module, we’ll cover:

  • Conditional tags
  • Iteration tags
  • Variable tags
  • Template tags
  • Operators and order of operations 
  • Truthy and falsy

As you know, tags use programming logic to decide which dynamic content is displayed in your email. We’ve already experimented with the elsif/else tag to choose content for our email based on the value of a key

Code{% if pet.pet_type == “dog” %}
Ruff ruff! We’ve received your order!
{% elsif pet.pet_type == “cat” %}
Meow! We’ve received your order!
{% else %}
Hooray! We’ve received your order!
{% endif %}
OutputRuff ruff! We’ve received your order!

Just like filters, the tags you have available to you will depend on the flavor of Liquid you’re using, and there are lots of options. To give you a taste, here are some commonly used tags:

  • if executes a block of code if your specified condition is met
  • elsif / else allows you to set up alternative code for various conditions that might occur
  • for executes a block of code over and over again (also called “iteration”)
  • assign creates a temporary key or temporarily renames a key

Notation and operators

First, let’s recap the notation you’ll use for tags. As you learned in Module 2, tags are surrounded by single curly brackets and percentage signs, like this:

{% tag %}

And because tags use logic to determine what content is displayed, they often contain operators to define that logic:

!=does not equal
>greater than
<less than
>=greater than or equal to
<=less than or equal to
orlogical or
andlogical and
containsa particular string is present

A note about the “contains”operator: it can check for a particular string within a string or an array of strings. It can only search strings!

You’ll see some of these operators in use as we dive into types of tags (and we’ll dig deeper into some of the trickier ones later). As a general rule, tags fall into four broad categories: conditional, iteration, variable, and template.