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 %}
assign statements at
the
very top of your template. This makes it easy to see at a glance which
data the template uses.
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 {{ user.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
|
{{ user.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.