Conditional tags

Conditional tags allow you to set conditions that determine whether Liquid code is executed or not. In the example above, we used the elsif/else conditional tag to set alternate messages depending on the type of pet—as well as what to output if there is no value stored in our database for the pet.pet_type key

We can also use conditional tags for simpler logic. Let’s use the if tag to check the value of the customer.purchased key. The customer.purchased key is an array, and the array is composed of strings representing the names of the products the customer has purchased, so we can use the contains operator to check for the presence of “bowl:”

Code (key and tag){% if customer.purchased contains “bowl” %}
You’ve already purchased a bowl. Why not get a bowl holder?
{% endif %}
Output You already purchased a bowl. Why not get a bowl holder?

Practice problem: conditional tags

Let’s practice with the elsif/else tag and the customer.purchased.most_recent key.

The customer.purchased.most_recent key contains a single string: the name of the most recent product purchased.

Offer a series of alternate messages based on the value of  customer.purchased.most_recent. If the most recent purchase is a leash, output, “Keep your beloved pet safe and sound with a tag.” If the most recent purchase is a bowl, output, “Make supper simpler with our durable bowl-holder.” If it’s neither, output, “Check out our new products!” 

Select the right Liquid code from the options below:

  1. {% if customer.purchased.most_recent == “leash” %}

Keep your beloved pet safe and sound with a tag.

{% elseif customer.purchased.most_recent == “bowl” %}

Make supper simpler with our durable bowl-holder.

{% else %}

Check out our new products!

{% endif %}

  1. {% if customer.purchased.most_recent == “leash” %}

Keep your beloved pet safe and sound with a tag.

{% elsif customer.purchased.most_recent == “bowl” %}

Make supper simpler with our durable bowl-holder.

{% else %}

Check out our new products!

{% endif %}

  1. {% if customer.purchased.most_recent = “leash” %}

Keep your beloved pet safe and sound with a tag.

{% elsif customer.purchased.most_recent = “bowl” %}

Make supper simpler with our durable bowl-holder.

{% else %}

Check out our new products!

{% endif %}

  1. {% if customer.purchased.most_recent == “leash” %}

Keep your beloved pet safe and sound with a tag.

{% elsif customer.purchased.most_recent == “bowl” %}

Make supper simpler with our durable bowl-holder.

{% else %}

Check out our new products!

The correct answer is B!

Let’s run the code. We’ll assume the most recent purchase was a tag:

Code (key and tag){% if customer.purchased.most_recent == “leash” %}
Keep your beloved pet safe and sound with a tag.
{% elsif customer.purchased.most_recent == “bowl” %}
Make supper simpler with our durable bowl-holder.
{% else %}
Check out our new products!
{% endif %}
OutputCheck out our new products!

Why don’t the other options work?

a. This is a sneaky spelling problem—the second part of the tag is spelled elsif, not elseif!

c. The operator is incorrect. In Liquid, equals is written as ==, not =.

d. Missing the last line of the tag: {% endif %}