Prompt
How do I support templates and variables in notification messages?
Latest observation
Oct 14, 2026
OpenAI APIWeb search: off
A good way to support templates and variables in notification messages is to separate:
- the message definition
- the data used to fill it in at send time
1) Use a template format
Store message text with placeholders, for example:
"Hello {{name}}, your order {{orderId}} has shipped.""Hi {name}, your balance is {balance}."
Common choices:
- Mustache / Handlebars-style:
{{name}} - Python-style:
{name} - Liquid:
{{ customer.name }}
Pick one and use it consistently.
2) Pass variables when sending
When sending a notification, provide a variables object/map:
{
"template": "Hello {{name}}, your order {{orderId}} has shipped.",
"variables": {
"name": "Ada",
"orderId": "12345"
}
}
Rendered output:
Hello Ada, your order 12345 has shipped.
3) Render server-side
Usually best to render the template on the backend before dispatching the notification. Benefits:
- consistent output
- easier validation
- safer than letting clients render arbitrary templates
- simpler auditing/logging
4) Support defaults and missing values
Decide what happens if a variable is missing:
- fail the send
- leave placeholder unchanged
- substitute a default value
Example:
{{name | default:"there"}}
5) Keep templates editable but controlled
If non-developers will edit notification text:
- store templates in a database or CMS
- expose a template editor with preview
- restrict allowed variables per template
Example metadata:
{
"id": "order_shipped",
"body": "Hello {{name}}, your order {{orderId}} has shipped.",
"allowedVariables": ["name", "orderId"]
}
6) Validate variables
Before rendering:
- ensure required variables are present
- ensure types are correct
- sanitize any user-provided content if needed
7) Preview before sending
A preview endpoint helps:
{
"templateId": "order_shipped",
"variables": {
"name": "Ada",
"orderId": "12345"
}
}
Return the rendered message so users can confirm it.
8) Example implementation idea
Template
Hi {{firstName}}, your verification code is {{code}}.
Send data
{
"firstName": "Maya",
"code": "839201"
}
Rendered
Hi Maya, your verification code is 839201.
9) If you need advanced logic
If you need conditionals, loops, or localization, choose a richer templating engine:
- Handlebars
- Liquid
- Jinja2 (Python)
- EJS (JavaScript)
Example conditional:
Hello {{name}}{{#if premium}}, thanks for being a premium member{{/if}}.
If you want, I can also show:
- a database schema for templated notifications
- a Node.js / Python / Java example
- or a recommended API design for sending templated messages