Sub topic 7
Showing different content with conditional logic
This chapter assumes you can insert merge tags with fallback values and know which contact fields your platform stores. Examples use Liquid-style syntax. Tag names and operators vary by platform, so check your platform's documentation.
What conditional logic does
A merge tag swaps in a value. A conditional block goes further and decides which content appears. You test a contact attribute, and the platform renders only the branch that matches. One template can then serve several kinds of reader.
The test runs for each recipient when the email is rendered. Contacts who don't match a branch never see its text.
The if / elsif / else structure
This block shows one of three messages depending 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 platform checks the conditions from top to bottom and renders the first one that is true. It skips the rest. The else branch catches every contact who matched nothing above it.
Always write the else branch
The else branch is the most important part for reliability. Contacts often have a blank plan, a value you didn't expect such as "Pro " with a trailing space, or no value at all. Without an else branch, those contacts get an empty gap in the email.
Write the else content so it reads well on its own. In the example, "Explore our plans." works for anyone.
Common comparisons
| Goal | Liquid-style example |
|---|---|
| Equals | `customer.plan == "pro"` |
| Not equal | `customer.country != "US"` |
| Number comparison | `customer.orders_count > 3` |
| Text contains | `customer.email contains "@example.com"` |
| Field is filled in | `customer.city != blank` |
Combine tests with and and or, for example customer.plan == "pro" and customer.orders_count > 3. Keep combined tests short. Complex rules are easy to get wrong and hard to preview. If a rule needs several conditions, consider defining it as a segment instead (a later chapter covers this).
Common errors
- Case and spelling mismatches. Comparisons are usually case-sensitive, so
"Pro"does not equal"pro". Clean the data or compare against the exact stored values. - Comparing numbers as text. A field stored as text may sort
"10"before"9". Confirm the field type before using>or<. - Missing end tags. Every
{% if %}needs its{% endif %}. A missing one can break rendering or print raw tag text to subscribers. - Assuming the field exists. A missing field often evaluates as empty, so it falls through to
else. That is the behaviour you want, but only ifelseis written well. - Order of branches. Put the most specific condition first. A broad test placed first hides the narrower ones below it.
Where conditionals fit
Use a conditional for small differences inside shared copy, such as one sentence or one button. If whole sections differ between audiences, dynamic content blocks (covered later) are easier to manage than a long chain of conditions in the body.
Exercise
A contact's plan field contains "Pro" (capital P). Your template tests customer.plan == "pro" and has an else branch reading "Explore our plans." What does this contact see, and how would you fix it?
Show answer
The contact sees "Explore our plans." because the comparison is case-sensitive, so "Pro" does not equal "pro" and the else branch runs. Fix it by standardising the stored values to lowercase, or by comparing against the exact stored value "Pro". Some platforms also offer a downcase filter, such as customer.plan | downcase == "pro".