Sub topic 7
Showing different content with conditional logic
This chapter assumes you can insert merge tags with fallbacks and that you know which contact fields your platform stores. Merge tags change a word. Conditional logic changes which content a contact receives. You write several versions of a passage in one template, and the platform picks one for each recipient when it renders the email.
How conditional content works
A conditional block has three parts: a test, one or more branches, and an optional fallback branch. The platform evaluates the test against the contact's data and renders only the branch that matches. The other branches never appear in the sent email.
The platform tests branches from top to bottom and stops at the first match. Put the most specific test first. Always include the final fallback branch. It catches every contact your earlier tests did not anticipate.
The basic if / elsif / else block
This Liquid-style example shows one of three messages based on the contact's plan.
{% if customer.plan == "pro" %}
Your Pro plan includes priority support.
{% elsif customer.plan == "free" %}
Upgrade to Pro to get priority support.
{% else %}
Explore our plans.
{% endif %}The pieces work like this:
{% if ... %}opens the block and holds the first test.{% elsif ... %}adds further tests. You can use as many as you need.{% else %}runs when no test matched. It takes no condition.{% endif %}closes the block. A missingendifis one of the most common template errors.
The {% %} delimiters run logic and print nothing themselves. The {{ }} delimiters print a value. Mixing them up is a common beginner slip.
Syntax differences between platforms
The idea is the same everywhere, but the keywords differ. Check your platform's documentation before you copy a template.
| Platform style | Else-if keyword | Closing keyword | Notes |
|---|---|---|---|
| Liquid (Shopify, Klaviyo-style variants) | `elsif` | `endif` | Variable names depend on the platform |
| Jinja2 and Django-style | `elif` | `endif` | Python-like syntax |
| Handlebars-style | `{{else if}}` | `{{/if}}` | Only simple truthiness tests, so comparisons often need helper functions |
| Visual builders | None | None | You pick the rule from a menu and the tool writes the code |
If your platform offers a visual rule builder, you may never see this syntax. Understanding it still helps you reason about what the builder does.
Comparison operators and combining tests
Conditions use operators. Liquid supports these:
| Operator | Meaning | Example |
|---|---|---|
| `==` | Equal to | `customer.plan == "pro"` |
| `!=` | Not equal to | `customer.country != "US"` |
| `>` `<` `>=` `<=` | Numeric comparison | `customer.orders_count >= 3` |
| `contains` | A string contains text, or an array contains an item | `customer.tags contains "vip"` |
| `and` | Both tests must be true | `a == 1 and b == 2` |
| `or` | Either test may be true | `a == 1 or b == 2` |
Here is a block that combines tests:
{% if customer.orders_count >= 3 and customer.country == "GB" %}
Thank you for being a regular. Delivery to the UK is free for you.
{% elsif customer.tags contains "vip" %}
As a VIP, you get early access to the sale.
{% endif %}Gotchas with combined tests
- No parentheses in Liquid. Liquid evaluates
andandorfrom right to left and does not support grouping. A test such asa or b and cmay not behave as you expect. When you need complex grouping, nest separateifblocks instead, or build a segment (see the segmentation chapter) and test membership. - Comparing numbers with text. If your platform stores
orders_countas the text"3", the comparison>= 3may fail or behave unexpectedly. Check the field type in your platform. - Case sensitivity.
"Pro"and"pro"are different strings in most platforms. Normalise values in your data (see the data quality chapter) or test for each spelling.
Handling empty and missing values
Conditional logic is often how you handle missing data when a fallback value is not enough. A fallback replaces one word. A condition can drop a whole sentence.
Many Liquid implementations treat only nil and false as false. An empty string counts as true. So {% if customer.first_name %} can run its branch even when the first name is blank. Compare against blank instead:
{% if customer.first_name != blank %}
Hi {{ customer.first_name }},
{% else %}
Hello,
{% endif %}blank is available in Shopify's Liquid. Some other platforms use a different test, such as != "" or a built-in "is empty" check. This is a platform-specific detail you must verify.
Missing fields also behave differently by engine. A missing field often renders as nothing, and a comparison against it fails quietly, so the else branch runs. That is why the else branch matters.
Seeing the logic run in Python
You can check the control flow outside your email platform with Jinja2. Jinja2 uses elif where Liquid uses elsif. The structure is otherwise the same.
from jinja2 import Environment
env = Environment()
template = env.from_string(
'{% if plan == "pro" %}Priority support included.'
'{% elif plan == "free" %}Upgrade to Pro.'
'{% else %}Explore our plans.{% endif %}'
)
contacts = [{"plan": "pro"}, {"plan": "free"}, {"plan": "enterprise"}, {}]
for contact in contacts:
print(repr(template.render(**contact)))The output is:
'Priority support included.'
'Upgrade to Pro.'
'Explore our plans.'
'Explore our plans.'The unknown plan "enterprise" and the contact with no plan at all both fall through to the else branch. Without that branch, both contacts would receive a blank space where the message should be.
Choosing between case statements and if chains
When one field has several known values, a case block can be easier to read than a long elsif chain:
{% case customer.language %}
{% when "fr" %}
Merci pour votre commande.
{% when "de" %}
Danke für Ihre Bestellung.
{% else %}
Thank you for your order.
{% endcase %}Use case when you compare one field to several exact values. Use if when you compare different fields or need ranges and combined tests.
Design rules that prevent embarrassing sends
Conditional content multiplies the number of versions of your email. Three branches on plan and three on country make nine possible combinations. Most failures come from versions nobody looked at.
- Always write the else branch. Treat it as the version for a contact you know nothing about. It should read well on its own.
- Keep branches parallel. Give each branch a similar length and structure so the layout holds up in every version.
- Limit the number of conditions. Past two or three independent conditions in one email, the number of versions grows faster than you can test. Move large differences into separate campaigns or segments.
- Avoid conditions that depend on unreliable fields. If a field is filled in for only half your contacts, half your audience gets the generic branch. Check field coverage before you build logic on it.
- Do not reveal sensitive inferences. A branch such as "Because you are over 50..." can feel intrusive even when the data is accurate. See the consent and privacy chapter.
Common errors
| Symptom | Likely cause |
|---|---|
| The raw `{% if ... %}` text appears in the sent email | The platform does not support that syntax, or the tag was inserted inside a field the platform does not process |
| Every contact sees the same branch | The field name is misspelled, so the value is always missing and the `else` branch runs |
| A blank line or gap where the block was | Whitespace around the tags is preserved, so trim it or place the tags on the same line as the text |
| Empty first names pass an `if first_name` test | Empty strings are truthy in Liquid, so compare with `blank` or `""` |
| Template save fails | An unclosed block, or `elif` used where the platform expects `elsif` |
Practice
Exercise
A contact has customer.first_name set to an empty string. Your template says {% if customer.first_name %}Hi {{ customer.first_name }},{% else %}Hello,{% endif %}. In standard Liquid, which greeting does the contact see, and how would you change the test to get the other behaviour you intended?
Show answer
The contact sees "Hi ," because Liquid treats an empty string as true, so the first branch runs and prints nothing for the name. Change the test to {% if customer.first_name != blank %}, or compare with an empty string, so that an empty value takes the else branch and shows "Hello,".
Exercise
Write a Liquid block that shows "Free shipping on your next order." to contacts whose customer.orders_count is 5 or more, shows "Order 5 times to unlock free shipping." to contacts with 1 to 4 orders, and shows "Place your first order today." to everyone else.
Show answer
{% if customer.orderscount >= 5 %}Free shipping on your next order.{% elsif customer.orderscount >= 1 %}Order 5 times to unlock free shipping.{% else %}Place your first order today.{% endif %} The most specific test comes first. The elsif catches counts of 1 to 4, and the else covers contacts with zero orders or no value at all.