Sub topic 9
Modular content and safe fallbacks
This chapter assumes you know Jinja2-style templating from the previous chapter and have a segment label for each customer. It covers how to split a message into modules and how to make sure a module never renders broken.
Why modules
A message built as one big template per segment multiplies quickly. Ten segments and four channels give forty templates to maintain. A modular message is a layout made of slots, such as a hero banner, a product block and a footer. Each slot is filled by a content module, which is a small, self-contained piece of content with its own variants per segment.
Modules give you three things:
- You edit one module and every message that uses it updates.
- You can test one slot without touching the others.
- You can attach a fallback to each slot separately.
The fallback chain
A fallback is the content you show when the preferred content cannot be rendered. Personalisation fails in ordinary ways: a first name is missing, a product is out of stock, a segment is not assigned yet, or the model service times out. A safe design assumes these failures happen and defines what each slot shows when they do.
Implementation
The code below stores variants per module. It renders with StrictUndefined, so a missing field raises an error instead of printing an empty string. It also drops None and empty values from the profile, because Jinja2 would otherwise print the word "None".
from jinja2 import Environment, StrictUndefined, UndefinedError
env = Environment(undefined=StrictUndefined)
MODULES = {
"hero": {
"lapsed": "We saved your basket, {{ first_name }}.",
"default": "See what is new this week.",
},
"products": {
"browsed": "Still thinking about {{ last_category }}?",
"default": "Our best sellers this week.",
},
}
def render_module(name, segment, profile):
clean = {k: v for k, v in profile.items() if v not in (None, "")}
variants = MODULES[name]
for key in (segment, "default"):
text = variants.get(key)
if text is None:
continue
try:
return env.from_string(text).render(**clean)
except UndefinedError:
continue
return ""
print(render_module("hero", "lapsed", {"first_name": "Ana"}))
print(render_module("hero", "lapsed", {"first_name": None}))
print(render_module("products", "unassigned", {}))This prints:
We saved your basket, Ana.
See what is new this week.
Our best sellers this week.The second call shows a missing name falling back to the default hero. The third shows an unknown segment doing the same.
Design rules
- Give every module a default variant that needs no personal data. Review it as carefully as the personalised ones, because it will reach many customers.
- Prefer replacing a whole variant over patching one field. "Hi there, we saved your basket" reads worse than a clean default.
- Decide per module whether an empty slot is acceptable. An empty footer breaks compliance. An empty recommendation block is usually fine.
- Log which variant each customer received, including fallbacks. Without this, a rising fallback rate goes unnoticed and your measurement later counts defaulted customers as personalised.
Common errors
| Error | Effect | Fix |
|---|---|---|
| Lenient undefined handling | Blank gaps such as "Hi , we saved..." | Use strict rendering and catch the error |
| No default variant | Empty or failed send | Require a default in every module |
| Fallback not logged | Skewed measurement | Record the variant key shown |
| Stale stock data | Promoting unavailable items | Check availability before rendering, with a fallback if it fails |
Exercise
A customer is in the segment "browsed" but has no last_category value. Using render_module above, what does the "products" module return, and why?
Show answer
It returns "Our best sellers this week." The "browsed" variant references last_category, which is missing, so StrictUndefined raises UndefinedError. The function catches it and moves on to the "default" variant, which needs no profile fields.