Prompt
How do I do template rendering for transactional emails without it becoming a mess?
Latest observation
A good way to keep transactional email templating from becoming a mess is to treat it like a small product, not a pile of string concatenations.
Core principles
1. Separate content, layout, and data
- Content: the actual text of the email
- Layout: header, footer, button styles, spacing, branding
- Data: user name, order number, links, dates, etc.
If these get mixed together, templates quickly become unmaintainable.
2. Use one reusable base layout
Most transactional emails should share:
- logo/header
- body container
- standard typography
- footer/legal text
Then individual emails only provide the unique body content.
Example structure:
base_email.htmlorder_confirmation.htmlpassword_reset.htmlinvoice_ready.html
Each specific template extends the base.
3. Keep templates dumb
Templates should mostly:
- display values
- include simple conditionals
- loop over collections
Avoid:
- business logic
- data transformation
- formatting decisions better handled in code
Do heavy logic before rendering and pass in a clean view model.
4. Create a presentation-layer view model
Instead of passing raw domain objects, prepare a dedicated email object:
{
"firstName": "Ava",
"orderNumber": "A1234",
"items": [
{"name": "Pro Plan", "price": "$20"}
],
"ctaUrl": "https://example.com/orders/A1234"
}
This keeps templates simple and stable even if your backend models change.
5. Standardize reusable components
Create components/partials for things like:
- buttons
- alert boxes
- tables
- section headers
- signature blocks
This avoids copying the same HTML everywhere.
If your template system supports it, use:
- partials/includes
- macros/helpers
- components/slots
6. Make email styling centralized
Inline CSS is often necessary for email clients, but that doesn’t mean styles should be duplicated everywhere.
A practical approach:
- define styles in one source
- compile/inject inline styles during build/send time
- keep class names and design tokens consistent
If possible, use a build step that converts modular templates into email-safe HTML.
7. Version templates intentionally
Transactional emails change often. Treat them like versioned assets:
- keep templates in source control
- review changes like code
- test before deployment
- consider template IDs/versions if your provider supports them
8. Preview and test automatically
Email rendering is notoriously inconsistent across clients.
Have automated checks for:
- HTML validity
- missing variables
- screenshots/previews in major clients if possible
- link correctness
- dark mode issues if relevant
At minimum, render templates in a preview environment with sample data.
9. Centralize localization
If you send emails in multiple languages:
- keep copy in translation files
- avoid hardcoding text in templates
- make date/number formatting part of the view model or helper layer
10. Enforce naming and folder conventions
A clean folder layout helps a lot:
emails/
base/
base.html
styles.css
components/
button.html
table.html
footer.html
order_confirmation/
html.html
subject.txt
preview.json
password_reset/
html.html
subject.txt
preview.json
This makes it easier to find, test, and maintain templates.
A practical pattern
A solid pattern is:
- Application code builds an
EmailViewModel - Template renderer injects it into a specific template
- Template extends a base layout
- Shared components are reused
- Subject line is managed separately from HTML
- Preview/test data is stored alongside the template
What to avoid
- giant templates with lots of copied markup
- embedding business logic in templates
- passing raw database objects directly
- hardcoding styles in every email
- mixing subject/body/html generation in one file
- using one-off templates for every tiny variation
Rule of thumb
If an email template is getting complex, ask:
- Can this be a reusable component?
- Can this data be precomputed in code?
- Can this be a shared base template?
- Can this copy live in localization files?
If the answer is yes, move it out of the template.
If you want, I can also show:
- a folder structure for email templates,
- a concrete example using Handlebars/Jinja/Liquid,
- or a “best practice” architecture for transactional emails in a real app.