The Case Tag
  • 04 Apr 2025
  • 3 minute read
  • Dark
    Light
  • PDF

The Case Tag

  • Dark
    Light
  • PDF

Article summary

In Liquid markup, the case tag lets you create an array of outputs that depend on the value of a given export.

In the case of a person reading your email: when they are an inquiry, they’ll receive a welcome message; when they are an applicant, they’ll instead find next steps on their application.

How to use case

case works like the conditional tag if, but with a little more structure:

  • Case blocks begin with the case, followed by the export you want Slate to evaluate. In our example, {% case export %}.

  • The exports are themselves followed by one or more when conditions, each corresponding to a potential value the export might have: {% when export-value-1 %}, {% when export-value-2 %}, and so on.

  • An optional (but recommended) else tag catches cases without any matching whens: {% else %}

  • Finally, it is closed with an endcase tag.

All together:

{% case export %}
    {% when export-value-1 %}
        Output for Export Value 1
    {% when export-value-2 %}
        Output for Export Value 2
    {% else %}
        Fallback if no when conditions are satisfied
{% endcase %}

Examples

Present different calls to action depending on the recipient’s person status, or else an opportunity to update their profile if they have no status:

{% case {{Person-Status}}%}
    {% when "Inquiry" %}
        Welcome {{Person-Preferred}}! We're so excited that you're interested in exploring Slate University. Check out our upcoming events!
    {% when "Applicant" %}
        Hello {{Person-Preferred}}! Feel free to reach out if you have any questions about your application!
    {% when "Current Student" %}
        Hello {{Person-Preferred}}, Check in with the Student Activities and Student Life Office to explore extracurriculars at Slate University
    {% when "Alumnit" %}
        Hello {{Person-Preferred}}, Stay connected with our alumni network for exclusive events and opportunities.
    {% else %}
        Welcome to Slate University! Please update your profile to get the most relevant information.
{% endcase %}

Show a student different material based on the value in their major field, or else give them an interdisciplinary course if they don’t have a major yet:

{% case student.major %}
    {% when "Computer Science" %}
        This course dives into advanced algorithms, perfect for Computer Science majors looking to deepen their programming skills.
    {% when "Business" %}
        This course covers the essentials of data analysis, ideal for Business majors interested in data-driven decision-making.
    {% when "Art" %}
        Explore digital tools for creating innovative art, a must-take for Art majors focusing on digital media.
    {% else %}
        Discover this interdisciplinary course, designed to appeal to students from all majors.
{% endcase %}

When to use case over if

case tags are designed specifically for scenarios where evaluation of a single export against multiple values is necessary.

if tags, on the other hand, require either if or elsif tags for each condition, which is wordier and can be harder to maintain.

Take the following examples. Both deliver the same outcome to the end user, one using case and when tags, the other using if and elsif tags:

{% case {{applicant_type}} %}
    {% when "full-time" %}
        Full-time student message
    {% when "part-time" %}
        Part-time student message
    {% when "alumni" %}
        Alumni message
    {% else %}
        Default message
{% endcase %}
{% if {{applicant_type}} == "full-time" %}
    Full-time student message
{% elsif {{applicant_type}} == "part-time" %}
    Part-time student message
{% elsif {{applicant_type}} == "alumni" %}
    Alumni message
{% else %}
    Default message
{% endif %}

Readability

In this example, the case tag is more readable and succinct as it compares the same export, like applicant_type, against its multiple values. The if tag makes you to call the applicant_type export every time.

Performance

The case tag has added performance benefits: it evaluates the expression once and matches it against all possible outcomes, whereas each individual if/elsif tag must be evaluated separately. While this might not be a critical factor, it can cause efficiency issues as your Liquid markup gets more complex.

Flexibility

Here, if has the edge. case tags allow for a fixed set of potential values to compare against, while if tags are better off evaluating complex conditions, or when those conditions are not just simple value comparisons.

📖 Further reading: See the Shopify documentation on case tags.


Was this article helpful?