Sub topic 9
Building dynamic content blocks
This chapter assumes you can insert merge tags with fallbacks, write a basic if/else conditional, and define a segment in your email platform. The examples use Liquid-style syntax. Other platforms use different syntax for the same ideas, and the notes below point out where.
What a dynamic block is
A merge tag swaps one value, such as a first name, into fixed text. A dynamic content block swaps a whole region of the email, such as a hero banner, a recommendation strip or a footer offer. Each recipient sees the variant that matches them.
A block has three parts:
- A slot. A named region in the template, such as
herooroffer. - Variants. Two or more versions of the content for that slot.
- Rules. Conditions that decide which variant a given contact gets.
One of the variants must be a default. It is shown when no rule matches, and it is the only thing between a contact with missing data and a blank hole in your layout.
Inline conditionals versus platform blocks
You can build dynamic blocks in two ways. Both give the same result for the reader, but they differ in who maintains them and where the rules live.
| Approach | Where the rules live | Strength | Weakness |
|---|---|---|---|
| Inline conditional (`{% if %}` in the HTML) | In the template code | Precise, version-controlled, can combine any fields | Needs someone comfortable with template syntax |
| Platform dynamic block (visual editor) | In the platform's block settings, often referencing saved segments | Marketers can edit without code, rules can be reused | Logic is hidden in the UI and is harder to review or diff |
Many platforms, such as HubSpot with smart content or Mailchimp with conditional content blocks, offer only the visual approach for some features. Others, such as Braze and Klaviyo, let you write the conditional yourself. Choose the visual approach when non-developers own the content. Choose inline code when the rules combine several fields or must be reviewed like code.
Segment-driven blocks
A segment is a saved audience rule, such as "plan is pro and has ordered in the last 90 days". A segment-driven block points each variant at a segment instead of repeating the rule.
This has two advantages. The rule is defined once and reused across campaigns. When the definition of "VIP" changes, every block that uses it changes with it.
It also has a cost: the segment is evaluated by the platform, and the timing of that evaluation matters. Some platforms evaluate membership when the campaign is sent. Others evaluate at render time, or use a cached membership list that refreshes on a schedule. A contact who joined the segment an hour ago may still see the old variant. Check your platform's documentation for when membership is calculated.
Writing the block inline
This Liquid example fills a hero slot. Each branch tests a field on the contact, and the last branch is the default.
{% if customer.plan == "pro" and customer.orders_count >= 5 %}
<h2>Thanks for being a loyal Pro customer</h2>
<p>Your early access to the spring range is open.</p>
{% elsif customer.plan == "pro" %}
<h2>Get more from your Pro plan</h2>
<p>Here are three features most Pro members try first.</p>
{% elsif customer.plan == "free" %}
<h2>See what Pro can do for you</h2>
<p>Upgrade this week and get priority support.</p>
{% else %}
<h2>Explore our plans</h2>
<p>Find the one that fits how you work.</p>
{% endif %}Three things in this example matter in practice.
- Order matters. The branches are checked from the top and the first match wins. The most specific rule (Pro and five or more orders) comes first. If you put
plan == "pro"first, the loyal-customer branch would never run. - The
elsebranch is the default. It catches free-plan contacts with a typo in the field, contacts with an empty plan, and anyone you did not plan for. - Field names and operators vary. Mailchimp uses tags such as
*|IF:PLAN == pro|*. Klaviyo uses Django-style tags that look similar to Liquid. HubSpot uses HubL. The structure is the same: test, branch, default.
How a block gets resolved
The platform runs the same process for every recipient when it builds each message.
You can model this logic yourself, which is a useful way to test your rule order before you build it in the platform. The Python below returns the name of the first matching variant and falls back to a default.
RULES = [
("loyal_pro", lambda c: c.get("plan") == "pro" and c.get("orders_count", 0) >= 5),
("pro", lambda c: c.get("plan") == "pro"),
("free", lambda c: c.get("plan") == "free"),
]
DEFAULT = "generic"
def pick_variant(contact):
for name, test in RULES:
if test(contact):
return name
return DEFAULT
contacts = [
{"plan": "pro", "orders_count": 7},
{"plan": "pro", "orders_count": 1},
{"plan": "free"},
{"plan": ""},
{},
]
for contact in contacts:
print(contact, "->", pick_variant(contact))Running this prints loyal_pro, pro, free, generic and generic. The last two contacts have an empty or missing plan, and both land on the default instead of failing.
Designing variants that work together
A dynamic block changes the email's content, but the email still has to look like one coherent design. These points prevent the most common layout problems.
- Keep variants the same shape. If the loyal variant has an image and a two-line paragraph and the default is a single line, the rest of the email shifts in ways you did not test. Aim for similar height and the same structure.
- Give every variant a call to action. A variant with no link is a dead end, and you will not notice it unless you preview that case.
- Include the plain-text part. Most platforms send a plain-text version alongside the HTML. A conditional in the HTML does not change the plain-text part. Repeat the logic there, or the text version will show a different message than the HTML.
- Check the preheader. The preheader is the preview line shown next to the subject. If the block's content also drives the preheader, the same rules apply there.
- Match images to alt text. Each variant needs its own alt text, because a blocked image should still describe the right offer.
- Keep the number of variants small. Each extra variant is one more thing to write, preview and keep current. Three or four is typical. Beyond that, ask whether a merged field or a repeated list would do the job better.
Common failure modes
| Problem | Cause | Fix |
|---|---|---|
| Everyone sees the default | Field name misspelled, or value case differs (`Pro` versus `pro`) | Check the exact field and stored values, and normalise case when you collect data |
| A contact matches two segments and sees the wrong one | Rules overlap and the order is wrong | Put the most specific rule first and make rules mutually exclusive where you can |
| Blank space in the email | No default variant, or the default is empty | Always provide a default with real content |
| Variant is out of date | Segment membership is cached | Learn when your platform recalculates, and avoid last-minute segment changes before a send |
| HTML and plain text disagree | Only the HTML version was made dynamic | Add the same conditional to the text part |
| Block works in the editor but not in the inbox | Preview used sample data that matched a rule | Test with incomplete and unusual data, as the testing chapter describes |
Privacy limits on block rules
Dynamic blocks are persuasive because they can reflect what you know about a person. That makes it easy to use data you should not. Base rules on data the contact gave you for this purpose, or that your privacy notice covers. A block that says "Because you looked at running shoes" depends on browsing data, so it needs the consent described in the privacy chapter. If the rule cannot be explained plainly to the recipient, reconsider it.
Exercise
A hero block has three rules in this order: (1) plan is "pro", (2) plan is "pro" and orders_count is at least 5, (3) plan is "free". A Pro customer with 12 orders gets the plain Pro message, not the loyal message. What is wrong, how do you fix it, and what else should you add?
Show answer
The rules are in the wrong order. The first match wins, so rule 1 catches every Pro customer before rule 2 can run. Move the more specific rule (Pro and at least 5 orders) above the general Pro rule. You should also add a default variant for contacts who match none of the rules, such as those with an empty plan, and repeat the same logic in the plain-text part of the email.