Sub topic 5
Keeping Data Clean So Merged Text Reads Well
This chapter assumes you can insert a merge tag, you know what a fallback value is, and you know what contact fields are. Fallbacks protect you when a field is empty. They do nothing when the field holds something wrong, such as [email protected] in the first-name field. This chapter covers how to prevent, detect, and repair bad values before they reach a subscriber.
Why a merge tag shows your data quality
A merge tag copies whatever is in the field into your sentence. If the field holds JOHN, the email says "Hi JOHN,". If it holds N/A, the email says "Hi N/A,". Template logic can't fix this on its own, because the template can't tell that N/A is not a name.
Most defects fall into a few groups:
| Symptom in the email | Typical cause | Where to fix it |
|---|---|---|
| `Hi JOHN,` or `Hi john,` | Inconsistent capitalisation at sign-up | Normalise on import or in the template |
| `Hi Sam ,` | Leading, trailing, or doubled spaces | Trim whitespace on entry |
| `Hi N/A,` or `Hi test,` | Placeholder values typed into forms | Filter out placeholders |
| `Hi [email protected],` | Email typed into the name field | Reject values containing `@` |
| `Hi Sam Smith Jr,` | Full name stored in the first-name field | Split into separate fields |
| `Your total is 19.5` | Number stored without formatting | Format with a filter or store a display string |
| `Renews on 2026-12-03` | Raw ISO date inserted | Apply a date format |
Where to clean
You can clean data at three points, and each catches a different set of problems.
- At entry. Form validation rejects obviously wrong input, such as an email address in the name field. This is the cheapest fix because the bad value never exists.
- At import or sync. When you load a CSV or sync from a CRM, a cleaning step repairs the whole batch. Fixing the stored value helps every future campaign, not just one.
- In the template. Filters such as
striphandle whatever slipped through. Template fixes are local to one email and can't solve every case, so treat them as a safety net and not as the main defence.
Clean as early as you can. A value repaired in the stored field is repaired everywhere, including segments, reports, and other templates.
Normalising names in a pipeline
Do heavier cleaning outside the email platform, in the script or tool that prepares your import. This function trims whitespace, discards placeholders and values that look like email addresses or contain digits, and fixes case only when a name is entirely upper- or lowercase.
def clean_first_name(raw):
if raw is None:
return ""
name = " ".join(str(raw).split()) # trims and collapses spaces
placeholders = {"n/a", "na", "none", "null", "unknown", "test", "-"}
if name.lower() in placeholders:
return ""
if "@" in name or any(ch.isdigit() for ch in name):
return ""
if name.isupper() or name.islower():
name = name.title()
return name
samples = [" sam ", "ALICE", "N/A", "jean-luc", "McDonald", None,
"[email protected]", "Ana María"]
for value in samples:
print(repr(value), "->", repr(clean_first_name(value)))The output is 'Sam', 'Alice', '', 'Jean-Luc', 'McDonald', '', '', and 'Ana María', in that order. The function returns an empty string for anything it rejects. Your template's fallback then supplies "there", so cleaning and fallbacks work together. The cleaner removes the bad value, and the fallback fills the gap.
Notice what the function leaves alone. McDonald is mixed case, so it is assumed to be correct. If the function applied title() to everything, McDonald would become Mcdonald, which is a worse error than the one you were fixing.
Defending in the template
Some cleaning is possible inside the template. Whitespace is the most important case, because of how default behaves in Liquid. It treats nil, false, and an empty string as missing. A string containing only a space is not empty, so the fallback never triggers and the email says "Hi ,". Trim first, then apply the default:
Hi {{ customer.first_name | strip | default: "there" }},Avoid using capitalize as a general repair. In Liquid it uppercases the first letter and lowercases the rest, so McDonald becomes Mcdonald. Other platforms differ. Some offer a title-case filter, and some have no case filters at all. Check your platform's filter list before relying on one.
If your platform supports it, you can also test for a usable value before inserting it. This is useful when the surrounding text only makes sense with the value present:
{% assign city = customer.city | strip %}
{% if city != blank %}
We have a store near you in {{ city }}.
{% else %}
Find a store near you.
{% endif %}Here blank is a Liquid keyword that matches empty strings, whitespace-only strings, and nil. This is often a better design than a fallback, because a sentence like "a store near you in your area" reads poorly with a substitute word.
Formatting dates, numbers, and currency
Data can be correct and still read badly. A stored date such as 2026-12-03 is accurate, but most readers prefer "3 December 2026". Use the platform's date filter:
Your plan renews on {{ customer.renewal_date | date: "%d %B %Y" }}.The format codes follow strftime, which is common but not universal. Some platforms use their own pattern letters such as dd MMMM yyyy. A date filter also fails quietly if the stored value is not a real date. If the field contains text such as "next Friday", you may get the text back unchanged, or an error, depending on the platform.
Numbers cause two recurring problems:
- Missing trailing zeros. A float of
19.5prints as "19.5", not "19.50". In Python,f"{19.5:.2f}"gives19.50. In a template, use your platform's money filter or send a preformatted string field. - Locale. The same amount is written
1,234.50in the US and1.234,50in Germany. Decide which locale each contact belongs to, and store a locale field if you send in several.
Store currency as a number plus a separate currency code. A single field containing "£19.50" can't be sorted, summed, or reformatted for another locale.
Structure problems: one field, several meanings
Some defects can't be fixed with filters, because the data model itself is wrong.
- Full name in one field. Splitting "Maria Garcia Lopez" into first and last is unreliable. Ask for the names separately at sign-up and keep a preferred-name field if you need it.
- Free-text fields used for categories. If
plancontains "Pro", "pro", "PRO ", and "Professional", the conditioncustomer.plan == "pro"from the conditional-content chapter matches only one of them. Use a fixed list of allowed values, or a dropdown, for any field a condition tests. - Duplicate contacts. Two records for one person mean two emails, possibly with different names. Merge duplicates by email address, compared in lowercase.
A practical audit routine
Run this routine before a large campaign, and after any import:
- Export the fields your template uses.
- Count the empty values for each one. A sudden rise after an import usually means a column mapping error.
- List the most common values in each field that conditions test. Unexpected variants (
Pro,PRO,pro) show up immediately. - Sort the first-name field and read the top and bottom of the list. Placeholders, emails, and symbols cluster at the ends.
- Search for values containing
@, digits, or the words "test", "n/a", and "unknown" in name fields. - Fix problems in the source data first, then re-import. Patching only the template leaves the stored data wrong for every other use.
Later chapters cover how to preview a message with these edge cases. For now, the audit gives you a list of the incomplete and awkward records to test with.
Exercise
A contact's first-name field contains a single space, and your template uses Hi {{ customer.first_name | default: "there" }},. The email says "Hi ,". Why did the fallback not apply, and what change fixes it?
Show answer
In Liquid, default only triggers for nil, false, or an empty string. A single space is a non-empty string, so the value is kept. Trim the value before applying the default: {{ customer.first_name | strip | default: "there" }}. After strip, the value is an empty string and the fallback applies.
Exercise
Your cleaning script applies title() to every first name. Name one kind of name it makes worse, and describe a safer rule.
Show answer
Names that are correctly mixed case, such as McDonald or DeShawn, become Mcdonald and Deshawn. A safer rule changes the case only when the whole value is entirely uppercase or entirely lowercase, and leaves mixed-case values unchanged.
Exercise
Your conditional block tests customer.plan == "pro", but the plan field contains "Pro", "PRO", and "pro " in different records. What are two ways to fix this?
Show answer
Clean the stored values to one allowed value, for example by trimming and lowercasing on import, and restrict future input to a fixed list. Alternatively, normalise inside the template before comparing, for example {% assign plan = customer.plan | strip | downcase %} and then test plan == "pro". Fixing the stored data is better because segments and reports also benefit.