Sub topic 3
Inserting merge tags
This chapter assumes you can send a campaign in an email platform and know what contacts, lists and fields are. Examples use Liquid-style syntax. Other platforms differ in the delimiters and field names, so check your platform's documentation.
What a merge tag is
A merge tag is a placeholder in your template. When the platform sends the email, it replaces each tag with a value from the recipient's contact record. One template produces many different emails.
A tag has three parts:
- The delimiters, such as
{{and}}, which mark the start and end of the tag. - The field path, such as
customer.first_name, which names the object and the field to read. - Optional filters, added after a pipe character (
|), which change the value before it is inserted.
Other platforms use other delimiters. Mailchimp uses *|FNAME|*, and Handlebars-based tools use {{first_name}}. The idea is the same.
How the platform renders a tag
The platform renders the template once per recipient. It looks up the field, applies any filters, and writes the result into the text.
Inserting a tag
Most editors give you two ways to insert a tag. You can pick a field from an "insert merge tag" menu, or you can type the tag by hand. The menu avoids typing mistakes, so use it when it is available.
Here is a greeting that reads the first name:
Hi {{ customer.first_name | default: "there" }},
Thanks for being with us.If first_name is "Sam", the email says "Hi Sam,". If the field is empty, it says "Hi there,". The default filter is covered in the next chapter, but include one from the start so a blank field never produces "Hi ,".
Matching tags to fields
A tag only works if its field path matches a real field in your contact data. Field names come from your data sources, such as sign-up forms, imports and integrations. Look at the exact name in your platform's field list before you write the tag.
| Problem | Example | Result |
|---|---|---|
| Wrong name | `{{ customer.firstname }}` when the field is `first_name` | Blank output, or an error |
| Wrong case | `{{ Customer.First_Name }}` | Often blank, because many platforms are case-sensitive |
| Missing spaces or braces | `{{ customer.first_name }` | Raw text appears in the email |
| Field is empty for this contact | `first_name` is blank | Blank output unless you set a fallback |
Using filters
Filters change a value before it is inserted. They are chained from left to right.
Hi {{ customer.first_name | capitalize | default: "there" }},
Your order total is {{ order.total | round: 2 }}.Here capitalize turns "sam" into "Sam" before the fallback check. Filter names vary by platform, so confirm which ones yours supports.
Rendering a template locally
You can test merge-tag logic outside your platform with Jinja2, a Python template engine. Its syntax is close to Liquid for simple tags. By default Jinja2 prints a blank for a missing field. StrictUndefined makes it raise an error instead, which exposes mistakes while you test.
from jinja2 import Environment, StrictUndefined
env = Environment(undefined=StrictUndefined)
template = env.from_string("Hi {{ first_name | default('there', true) }}, your plan is {{ plan }}.")
print(template.render(first_name="", plan="Pro"))
try:
template.render(first_name="Sam")
except Exception as error:
print(type(error).__name__)The first call prints Hi there, your plan is Pro. The second call has no plan, so it prints UndefinedError. The true argument to default makes an empty string count as missing, not only an absent field.
Common errors to check
- Typing the tag instead of inserting it from the field menu.
- Copying a tag from another platform without changing its syntax.
- Pasting from a word processor, which can replace straight quotes with curly quotes and break filter arguments.
- Using a field that only some contacts have, with no fallback.
Exercise
A template contains "Hi {{ customer.firstname }}," but the contact field is named first_name. Contacts see "Hi ," in the sent email. What is the cause, and how do you fix it?
Show answer
The field path does not match the real field name, so the renderer finds no value and outputs a blank. Change the tag to {{ customer.first_name | default: "there" }}, ideally by inserting it from the platform's field menu, and add a fallback for contacts whose first name is empty.