Sub topic 5
Keeping data clean so merged text reads well
This chapter assumes you can insert a merge tag and set a fallback value, as covered earlier. A merge tag only inserts what is stored in the contact field. If the field holds SAM, sam , or N/A, the email shows exactly that.
Why fallbacks are not enough
A fallback covers only an empty or missing field. It does not cover a field that has a value that is wrong or badly formatted. These are the common problems:
| Problem | Stored value | What the reader sees |
|---|---|---|
| Wrong case | `SAM` | "Hi SAM," |
| Stray whitespace | `Sam ` | "Hi Sam ," |
| Placeholder text | `N/A`, `test`, `-` | "Hi N/A," |
| Full name in one field | `Sam Rivera` | "Hi Sam Rivera," |
| Email used as name | `[email protected]` | "Hi [email protected]," |
Each case is a data problem, so the fix belongs in the data, not in the template.
Clean at the point of entry
Fixing data when it arrives is cheaper than fixing it at send time. Apply these rules at every entry point, such as signup forms, imports, and integrations:
- Trim leading and trailing spaces.
- Convert empty strings and known placeholders to a true empty value, so your fallback applies.
- Split full names into first and last name fields where you can.
- Store dates, currencies, and phone numbers in one consistent format.
- Validate on input. Reject a first name that contains
@or digits.
Cleaning in the template as a safety net
Most Liquid-style platforms offer text filters. Use them for light protection, though they can't repair bad data.
Hi {{ customer.first_name | strip | capitalize | default: "there" }},Filter order matters. Here strip removes spaces first, so a value that was only spaces becomes empty and the default filter can replace it.
Be careful with capitalize. In Liquid it makes the first letter uppercase and the rest lowercase, so McDonald becomes Mcdonald and de la Cruz is changed too. Many teams normalise only values that are entirely uppercase or entirely lowercase. Do that when the data is stored, not in the template.
Filter names differ by platform. Mailchimp, Klaviyo, and others use different syntax or have no case filters, so check your platform's documentation.
A cleaning step you can run on imports
If your platform lets you clean a file before import, a short script catches most of these problems.
PLACEHOLDERS = {"", "n/a", "na", "none", "null", "test", "-"}
def clean_first_name(raw):
name = (raw or "").strip()
if name.lower() in PLACEHOLDERS or "@" in name or any(c.isdigit() for c in name):
return ""
if name.isupper() or name.islower():
name = name.title()
return name.split(" ")[0] if " " in name else name
for value in ["SAM", " sam ", "N/A", "[email protected]", "Sam Rivera", "McDonald", None]:
print(repr(value), "->", repr(clean_first_name(value)))The function returns an empty string for unusable values, so the template's fallback takes over. It leaves mixed-case names such as McDonald unchanged. Taking the first word of a full name is a guess. It fails for first names with two parts, such as "Mary Ann", so prefer a separate first name field when you can.
Check your data before sending
Before a campaign, export a sample and look for patterns. Sort the first name column and read the top and bottom, where blanks and symbols collect. Count rows that are empty, all uppercase, or contain @. A small fix in the source data usually removes a problem that would otherwise reach every subscriber.
Exercise
A contact's first name is stored as " JORDAN ". Your template uses {{ customer.first_name | default: "there" }}. What does the email say, and what would you change?
Show answer
It says "Hi JORDAN ," with the stray spaces and capitals, because the value is not empty and the default filter does not apply. Clean the stored value at entry by trimming it and converting it to "Jordan". As a safety net, add strip (and optionally capitalize) before default in the template.