As your templates grow more complex, you may find yourself repeating long attribute names or writing the same logic in multiple places. The assign tag and the default filter help you keep templates clean, readable, and easier to maintain.
The Assign Tag
The assign tag creates a reusable variable inside your template. Once assigned, you can reference the variable by its short name anywhere else in the template.
{% assign tier = customer.VIPSTAGE %}
After this line, you can use {{ tier }} or {% if tier == "Gold" %} instead of writing out customer.VIPSTAGE every time.
When to Use Assign
-
Avoid repeating long attribute names: Replace
customer.ECOM_FAVORITE_PRODUCT_CATEGORYwith a short variable likefav_category. - Clean up complex logic: Define your variables at the top of the template so conditions below are easier to read.
- Improve readability: Other team members can quickly understand what data is being used.
Example: Simplifying a Template with Assign
Without assign (harder to read):
{% if customer.VIPSTAGE == "Platinum" %}
Welcome back, Platinum member!
{% elsif customer.VIPSTAGE == "Gold" %}
Great to see you, Gold member!
{% else %}
Welcome! Join our loyalty programme today.
{% endif %}
With assign (cleaner):
{% assign tier = customer.VIPSTAGE %}
{% if tier == "Platinum" %}
Welcome back, Platinum member!
{% elsif tier == "Gold" %}
Great to see you, Gold member!
{% else %}
Welcome! Join our loyalty programme today.
{% endif %}
Assigning the Result of a Condition
You can also assign the outcome of a condition to a variable. This is useful when the same long condition is needed in more than one place: write it once, then reuse the short variable.
{% assign is_vip = false %}
{% if customer.VIPSTAGE == "Platinum" or customer.VIPSTAGE == "High Roller" %}
{% assign is_vip = true %}
{% endif %}
{% if is_vip %}
Your VIP concierge is standing by.
{% endif %}
Assign the variable a starting value first, then overwrite it when the condition is met. From that point on, {% if is_vip %} can be used anywhere in the template without repeating the full condition.
assign statements at the very top of your template. This makes it easy to see at a glance which data the template uses. See Whitespace Control for how to stop a block of assign statements from adding blank lines to your message.The Default Filter
The default filter provides a fallback value if data is missing or empty. If the attribute has data, it is shown. If it is empty or null, the default value is shown instead.
Will {{ customer.SUPPORTER | default: 'your team' }} get the win today?
- If SUPPORTER = "Tottenham": Will Tottenham get the win today?
- If SUPPORTER is empty or null: Will your team get the win today?
When to Use Default
- Personalization is optional: You want to include a name or preference if available, but the message still works without it.
- A simple value replacement is needed: You only need to swap in one fallback word or phrase rather than change the entire structure of the content.
- You want to avoid blank output: Without a default, a missing attribute renders as nothing, which can leave awkward gaps in your message.
Chaining Default with Other Filters
You can combine default with other filters. The default filter should come first so the fallback value is also transformed.
Hi {{ customer.first_name | default: 'there' | capitalize }}!
- If first_name = "sarah": Hi Sarah!
- If first_name is missing: Hi There!
Default vs. IF / ELSE: Which Should I Use?
Both default and if / else handle missing data, but they serve different purposes.
| Scenario | Use This | Example |
|---|---|---|
| Only a single value needs replacing |
default filter |
{{ customer.first_name | default: 'there' }} |
| The structure or layout of the content changes |
if / else
|
Show a completely different paragraph or section for VIP vs. non-VIP |
| Multiple outcomes based on different values |
if / elsif / else
|
Different messages for Platinum, Gold, and non-VIP customers |
default. If you are changing a whole sentence, paragraph, or section, use if / else.Whitespace Control
Liquid tags sit on their own lines in a template, but they render as nothing. The line breaks around them are still output, so a multi-line logic block can inject stray blank lines into the finished message.
Adding a hyphen inside the tag delimiters trims the whitespace and newlines around that tag. This works on both logic tags, {%- -%}, and output tags, {{- -}}.
{%- if customer.VIPSTAGE == "Platinum" -%}
...
{%- endif -%}
A hyphen on the left of the tag trims the whitespace before it; a hyphen on the right trims the whitespace after it. You can use one or both.