Templates and layouts
Templates
Section titled “Templates”A template is what you send by ID. It has:
| Field | Required | Notes |
|---|---|---|
| ID | Yes | welcome, password_reset. Starts with a lowercase letter, then a-z, 0-9 and _; up to 48 characters. Unique in your account. Can’t be changed later. |
| Name | Yes | A display name, like Welcome email. |
| Subject | Yes | Mustache. |
| HTML | Yes | Mustache. |
| Plain text | No | Mustache. If you leave it out, it’s generated from the final HTML. |
| Layout | No | One of your layouts to wrap the template in. |
Every change creates a new version, and old versions stay available in the dashboard’s history and through the Management API.
Variables and escaping
Section titled “Variables and escaping”Templates use Mustache. The data object you send fills them in:
<p>Hi {{name}},</p>{{#items}} <li>{{title}}: {{price}}</li>{{/items}}{{^items}} <p>Your cart is empty.</p>{{/items}}{{name}}is HTML-escaped in the HTML body, so data from your users can’t inject markup. Quotes are escaped too, so variables are safe inside attributes likehref="{{url}}". Slashes are not escaped, so links stay readable to spam filters.{{{name}}}(three braces) inserts the value without escaping. Only use it for HTML you generated yourself.- The subject is not HTML, so it’s never escaped, and line breaks in it are removed.
- Missing variables render as empty text.
When the email is sent, the CSS in <style> blocks is inlined into style attributes, because many email
clients ignore <style>.
Layouts
Section titled “Layouts”A layout holds what many templates share: the outer HTML, header, footer and styles. It has a {{> template}}
slot where the template’s content goes:
<!doctype html><html><body style="margin:0;background:#f4f4f5"> <table role="presentation" width="100%" style="max-width:600px;margin:0 auto;background:#fff"> <tr><td style="padding:24px;font-weight:bold">{{company}}</td></tr> <tr><td style="padding:0 24px 24px">{{> template}}</td></tr> <tr><td style="padding:16px 24px;font-size:12px">You received this email from {{company}}.</td></tr> </table></body></html>- The template is rendered first, then inserted at
{{> template}}exactly as rendered: it isn’t re-indented (so<pre>blocks stay intact) and its output isn’t rendered a second time. - The layout and the template get the same
data, so{{company}}works in both. - If the layout has a plain-text version, it must contain
{{> template}}too. {{> template}}is the only include there is: templates can’t include anything, and layouts can’t include other layouts.- A layout that templates use can’t be deleted until you reassign or delete those templates.
Previewing
Section titled “Previewing”The dashboard’s Preview tab renders as you type. From code, preview a saved template or layout, or an unsaved draft, with the Management API. Nothing is sent or stored when you preview.