What You’ll Learn in This Module
Module 12 covered if/elsif/else — the most common logic structure in Liquid. This module introduces two more: unless (which runs when a condition is false) and case/when (which matches a variable against a list of specific values). Both are tools for writing cleaner, more readable narrations.
unless — Do This If the Condition Is False
What is it?
The unless tag is the inverse of if. It runs the enclosed content when the condition is false (or when the value is nil/empty). Think of it as “do this unless the condition is true.”
Excel analogy: If
{% if %}is=IF(condition, "show this", ""), then{% unless %}is=IF(NOT(condition), "show this", ""). It flips the logic.
Why it matters in Cast
unless makes your intent clearer when you’re really thinking about the absence of something: “show this unless the field is blank,” “include this unless they’re on the Starter plan.” It reads more naturally than {% if field != blank %} in many situations.
Liquid syntax
{% unless condition %}
Content shown when condition is FALSE.
{% endunless %}
You can also add an else branch (though this is uncommon — if you need both branches, if/else is usually clearer):
{% unless condition %}
Content when false.
{% else %}
Content when true.
{% endunless %}
Real Cast examples
Show renewal date only when it exists:
{% unless renewal_date == blank %}
Your contract renews on {{ renewal_date | date: "%B %d, %Y" }}.
{% endunless %}
This reads naturally: “Unless the renewal date is blank, show it.” Compare with the if version:
{% if renewal_date != blank %}
Your contract renews on {{ renewal_date | date: "%B %d, %Y" }}.
{% endif %}
Both produce identical output. Choose whichever reads more clearly to you and your team.
Skip content for a specific tier:
{%- assign tier = product_tier | default: "" | strip -%}
{% unless tier == "Starter" %}
You have access to our advanced reporting suite.
{% endunless %}
This says: “Everyone except Starter tier sees this content.”
When to use unless vs. if
Use unless when… | Use if when… |
|---|---|
| You’re checking for the absence of something | You’re checking for the presence of something |
| The condition is a simple negative | The condition has multiple branches (elsif/else) |
unless X == blank reads naturally | The positive case is what you care about |
💡 Rule of thumb: If you find yourself writing {% if something != ... %}, consider whether {% unless something == ... %} reads better. But don’t overthink it — readability is the goal.
Common mistakes
❌ Using unless with complex conditions:
{% unless score < 60 and tier != "Enterprise" %}
Double negatives are hard to reason about. “Unless the score is NOT less than 60 AND the tier is NOT not Enterprise…” — confusing.
✅ Use if for complex conditions:
{% if score >= 60 or tier == "Enterprise" %}
❌ Adding elsif to an unless block:
{% unless tier == "Starter" %}
Advanced content.
{% elsif tier == "Professional" %} ← not supported in unless
unless does not support elsif. Use if/elsif/else instead.
✅ Switch to if for multi-branch logic:
{% if tier == "Enterprise" %}
Enterprise content.
{% elsif tier == "Professional" %}
Professional content.
{% else %}
Basic content.
{% endif %}
case/when — Matching Against Specific Values
What is it?
The case/when structure matches a single variable against a list of specific values and executes the content for the matching value. It’s the Liquid equivalent of a switch statement — cleaner and more readable than a chain of elsif blocks when you’re comparing the same variable against multiple possible values.
Excel analogy:
case/whenis like aSWITCHfunction:=SWITCH(A1, "Enterprise", "All features", "Professional", "Core features", "Contact CSM"). Each value gets its own result, with an optional default at the end.
Why it matters in Cast
Many CRM fields have a fixed set of possible values — product tiers, regions, plan types, support levels. When you need different narration content for each value, case/when is cleaner than a chain of {% if tier == "X" %}{% elsif tier == "Y" %}{% elsif tier == "Z" %}.
Liquid syntax
{% case variable %}
{% when "value1" %}
Content for value1.
{% when "value2" %}
Content for value2.
{% when "value3" %}
Content for value3.
{% else %}
Fallback content (none of the above).
{% endcase %}
The {% else %} branch is optional but recommended — it catches unexpected values.
Real Cast example — product tier messaging
{%- assign tier = product_tier | default: "" | strip -%}
{% case tier %}
{% when "Enterprise" %}
You have access to all features including advanced analytics,
dedicated support, and custom integrations.
{% when "Professional" %}
You have access to core features and standard reporting.
Talk to your CSM about Enterprise for advanced analytics.
{% when "Starter" %}
You have access to essential features.
Let's explore how upgrading could accelerate your success.
{% else %}
Contact your {{ "CSM" | cast_pronounce }} to explore available plans.
{% endcase %}
Compare this to the equivalent if/elsif chain:
{% if tier == "Enterprise" %}
...
{% elsif tier == "Professional" %}
...
{% elsif tier == "Starter" %}
...
{% else %}
...
{% endif %}
The case/when version is more readable when you have three or more specific values to match.
Real Cast example — region-based content
{%- assign region = contact_region | default: "" | strip | upcase -%}
{% case region %}
{% when "NAM" %}
Your North American support team is available 8 AM–8 PM EST.
{% when "EMEA" %}
Your EMEA support team is available 8 AM–6 PM CET.
{% when "APAC" %}
Your APAC support team is available 8 AM–6 PM SGT.
{% else %}
Your global support team is available 24/7 via email.
{% endcase %}
When to use case/when vs. if/elsif
Use case/when when… | Use if/elsif when… |
|---|---|
| Comparing one variable to 3+ specific values | Comparing multiple different variables |
| Values are exact matches (strings, numbers) | Conditions use ranges (>, <, >=) |
| Logic is “which category does this fall into?” | Logic is “is this above/below a threshold?” |
💡 Key limitation: case/when only supports exact equality (==). You cannot use >, <, contains, or and/or inside when clauses. For those, use if/elsif.
Multiple values in a single when
You can match multiple values with a comma-separated list using or:
{% case tier %}
{% when "Enterprise" or "Professional" %}
You have access to advanced features.
{% when "Starter" %}
You have access to essential features.
{% endcase %}
Common mistakes
❌ Forgetting endcase:
{% case tier %}
{% when "Enterprise" %}
Enterprise content.
Missing {% endcase %} causes an error.
✅ Always close with endcase:
{% case tier %}
{% when "Enterprise" %}
Enterprise content.
{% endcase %}
❌ Trying to use comparison operators in when:
{% case score %}
{% when > 80 %} ← NOT VALID
Excellent!
{% endcase %}
when only supports exact values, not ranges.
✅ Use if/elsif for range comparisons:
{% if score >= 80 %}
Excellent!
{% elsif score >= 60 %}
Good.
{% endif %}
❌ Comparing against a value with different casing:
{% case tier %}
{% when "enterprise" %} ← won't match "Enterprise"
case/when comparisons are case-sensitive.
✅ Normalize the variable before the case block:
{%- assign tier = product_tier | default: "" | strip | downcase -%}
{% case tier %}
{% when "enterprise" %}
Enterprise content.
{% endcase %}
❌ Forgetting the else fallback:
{% case tier %}
{% when "Enterprise" %}
Enterprise content.
{% when "Professional" %}
Professional content.
{% endcase %}
If tier is "Starter" or anything else, nothing is shown. The customer sees a gap.
✅ Always include an else branch:
{% case tier %}
{% when "Enterprise" %}
Enterprise content.
{% when "Professional" %}
Professional content.
{% else %}
Contact your CSM for plan details.
{% endcase %}
Choosing the Right Logic Structure — Summary
| Structure | Best For | Example |
|---|---|---|
if/endif | Simple yes/no check | {% if score >= 80 %} |
if/else | Two-branch decision | High score vs. low score |
if/elsif/else | Multiple threshold ranges | Score tiers (80+, 60+, below 60) |
unless | Checking for absence | {% unless date == blank %} |
case/when | Matching against specific values | Product tier, region, plan type |
Try It Yourself
Exercise: You have a variable support_level that can be "Gold", "Silver", "Bronze", or potentially empty. Write a case/when block that:
- Gold → “You have 24/7 priority support with a 1-hour response SLA.”
- Silver → “You have business hours support with a 4-hour response SLA.”
- Bronze → “You have email-only support with a 24-hour response SLA.”
- Empty or anything else → “Contact your CSM to discuss your support options.”
Include proper normalization and a blank check.
Click to reveal the answer
```liquid {%- assign level = support_level | default: "" | strip -%} {% case level %} {% when "Gold" %} You have 24/7 priority support with a 1-hour response SLA. {% when "Silver" %} You have business hours support with a 4-hour response SLA. {% when "Bronze" %} You have email-only support with a 24-hour response SLA. {% else %} Contact your {{ "CSM" | cast_pronounce }} to discuss your support options. {% endcase %} ``` Key details: - `default: ""` handles nil values - `strip` handles whitespace - The `else` branch catches both empty strings and unexpected values - Note: since the original values use title case ("Gold" not "gold"), we don't use `downcase` — but if you're unsure about casing from the CRM, normalizing is saferWhat’s Next
In Module 14, you’ll learn about loops — using for/endfor to iterate through arrays and display lists, numbered items, and repeated content blocks.
📖 Official documentation:
- Tags/Blocks: https://school.cast.app/liquid/liquid-blocks.html