Skip to content

Module 13: Logic — unless and case/when

unless — Do This If the Condition Is False

Section titled “unless — Do This If the Condition Is False”

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.”

{% raw %}> Excel analogy: If {% if %} is =IF(condition, "show this", ""), then {% unless %} is =IF(NOT(condition), "show this", ""). It flips the logic.{% endraw %}

{% raw %}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.{% endraw %}

{% raw %}
{% unless condition %}
Content shown when condition is FALSE.
{% endunless %}
{% endraw %}

You can also add an else branch (though this is uncommon — if you need both branches, if/else is usually clearer):

{% raw %}
{% unless condition %}
Content when false.
{% else %}
Content when true.
{% endunless %}
{% endraw %}

Show renewal date only when it exists:

{% raw %}
{% unless renewal_date == blank %}
Your contract renews on {{ renewal_date | date: "%B %d, %Y" }}.
{% endunless %}
{% endraw %}

This reads naturally: “Unless the renewal date is blank, show it.” Compare with the if version:

{% raw %}
Your contract renews on {{ renewal_date | date: "%B %d, %Y" }}.
{% endraw %}

Both produce identical output. Choose whichever reads more clearly to you and your team.

Skip content for a specific tier:

{% raw %}
{%- assign tier = product_tier | default: "" | strip -%}
{% unless tier == "Starter" %}
You have access to our advanced reporting suite.
{% endunless %}
{% endraw %}

This says: “Everyone except Starter tier sees this content.”

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

{% raw %}💡 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.{% endraw %}

Using unless with complex conditions:

{% raw %}
{% unless score < 60 and tier != "Enterprise" %}
{% endraw %}

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:

{% raw %}
{% endraw %}

Adding elsif to an unless block:

{% raw %}
{% unless tier == "Starter" %}
Advanced content.
{% elsif tier == "Professional" %} ← not supported in unless
{% endraw %}

unless does not support elsif. Use if/elsif/else instead.

Switch to if for multi-branch logic:

{% raw %}
Enterprise content.
{% elsif tier == "Professional" %}
Professional content.
{% else %}
Basic content.
{% endraw %}

case/when — Matching Against Specific Values

Section titled “case/when — Matching Against Specific Values”

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/when is like a SWITCH function: =SWITCH(A1, "Enterprise", "All features", "Professional", "Core features", "Contact CSM"). Each value gets its own result, with an optional default at the end.

{% raw %}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 &#123;% if tier == "X" %&#125;&#123;% elsif tier == "Y" %&#125;&#123;% elsif tier == "Z" %&#125;.{% endraw %}

{% raw %}
{% 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 %}
{% endraw %}

{% raw %}The &#123;% else %&#125; branch is optional but recommended — it catches unexpected values.{% endraw %}

Real Cast example — product tier messaging

Section titled “Real Cast example — product tier messaging”
{% raw %}
{%- 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 %}
{% endraw %}

Compare this to the equivalent if/elsif chain:

{% raw %}
...
{% elsif tier == "Professional" %}
...
{% elsif tier == "Starter" %}
...
{% else %}
...
{% endraw %}

The case/when version is more readable when you have three or more specific values to match.

Real Cast example — region-based content

Section titled “Real Cast example — region-based content”
{% raw %}
{%- 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 %}
{% endraw %}
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.

You can match multiple values with a comma-separated list using or:

{% raw %}
{% case tier %}
{% when "Enterprise" or "Professional" %}
You have access to advanced features.
{% when "Starter" %}
You have access to essential features.
{% endcase %}
{% endraw %}

Forgetting endcase:

{% raw %}
{% case tier %}
{% when "Enterprise" %}
Enterprise content.
{% endraw %}

{% raw %}Missing &#123;% endcase %&#125; causes an error.{% endraw %}

Always close with endcase:

{% raw %}
{% case tier %}
{% when "Enterprise" %}
Enterprise content.
{% endcase %}
{% endraw %}

Trying to use comparison operators in when:

{% raw %}
{% case score %}
{% when > 80 %} ← NOT VALID
Excellent!
{% endcase %}
{% endraw %}

when only supports exact values, not ranges.

Use if/elsif for range comparisons:

{% raw %}
Excellent!
{% elsif score >= 60 %}
Good.
{% endraw %}

Comparing against a value with different casing:

{% raw %}
{% case tier %}
{% when "enterprise" %} ← won't match "Enterprise"
{% endraw %}

case/when comparisons are case-sensitive.

Normalize the variable before the case block:

{% raw %}
{%- assign tier = product_tier | default: "" | strip | downcase -%}
{% case tier %}
{% when "enterprise" %}
Enterprise content.
{% endcase %}
{% endraw %}

Forgetting the else fallback:

{% raw %}
{% case tier %}
{% when "Enterprise" %}
Enterprise content.
{% when "Professional" %}
Professional content.
{% endcase %}
{% endraw %}

If tier is "Starter" or anything else, nothing is shown. The customer sees a gap.

Always include an else branch:

{% raw %}
{% case tier %}
{% when "Enterprise" %}
Enterprise content.
{% when "Professional" %}
Professional content.
{% else %}
Contact your CSM for plan details.
{% endcase %}
{% endraw %}

Choosing the Right Logic Structure — Summary

Section titled “Choosing the Right Logic Structure — Summary”
Structure Best For Example
{% raw %} if/endif Simple yes/no check
if/else Two-branch decision High score vs. low score
if/elsif/else Multiple threshold ranges Score tiers (80+, 60+, below 60)
{% raw %} unless Checking for absence
case/when Matching against specific values Product tier, region, plan type

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
{% raw %}
{%- 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 %}
{% endraw %}

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 safer

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: