Bouncewise

Bouncewise › Guides › Postmark template syntax: Mustachio, layouts and sending

Postmark guide

Postmark templates: Mustachio syntax, layouts and sending with a template

By the Bouncewise team · Updated · 4 min read · How we check guides

Short answer

Postmark templates use Mustachio, a Mustache-like language: {{ name }} inserts an HTML-escaped value, {{#name}}…{{/name}} renders a block only when the value is present (and scopes into it), {{^name}}…{{/name}} renders when it is missing, false, null or empty, and {{#each items}}…{{/each}} loops. There is no if/else keyword; sections and inverted sections do that job. Send with POST /email/withTemplate, giving TemplateId or TemplateAlias plus a TemplateModel with the values.

Mustachio syntax at a glance

SyntaxWhat it does
{{ person.first_name }}Inserts a value, HTML-encoded. Dots reach into objects.
{{#person}}…{{/person}}Scopes into person so you can write {{ first_name }}; skipped if person is missing, null, false or empty.
{{^person}}…{{/person}}Inverted group: renders only when the value is missing, null, false or empty.
{{#each items}}…{{/each}}Repeats the block for each element of an array.
{{ . }}The current value inside a section.
{{ ../company_name }}Goes up one level of the model; repeat ../ to go higher.
{{{ html }}} or {{& html }}Inserts without HTML encoding. Only for content you trust.
{{{ @content }}}In a layout: where the template's content goes.
{{{ pm:unsubscribe }}}Postmark's unsubscribe link; required on broadcast streams.

Variables work in the Subject too, for example Your {{ product_name }} receipt. When you send with a template you can't pass a Subject field; put a variable in the template's subject instead.

List of a Postmark server's templates with their names and aliases
A server's templates.

If/else in Postmark templates

Postmark's FAQ says it plainly: Mustachio doesn't have any if-else syntax built in. Pair a section with an inverted group on the same key:

{{#first_name}}Hi {{ . }},{{/first_name}}{{^first_name}}Hi there,{{/first_name}}

{{#trial_ends_at}}
  Your trial ends on {{ . }}.
{{/trial_ends_at}}
{{^trial_ends_at}}
  Thanks for being a paying customer.
{{/trial_ends_at}}

With {"first_name": "Ann"} this renders Hi Ann,; with the key missing, null or false it renders Hi there,. For a flag, send a boolean such as "is_trial": true and wrap blocks in {{#is_trial}} and {{^is_trial}}.

Two gotchas: there are no comparisons (status == "paid"), so compute booleans in your code before sending; and inside {{#key}} the scope changes to that value, so reach other fields with ../.

Loops and nested values

Use {{#each}} for arrays. Inside the loop each element is the scope, and ../ climbs back to the outer model:

<ul>
{{#each items}}
  <li>{{ name }} × {{ quantity }}{{#note}} ({{ . }}){{/note}} for {{ ../../customer_name }}</li>
{{/each}}
</ul>

The path depth follows Postmark's own example, which uses ../../ to reach a top-level value from inside {{#each}}. An empty array renders nothing, so add {{^items}}No items.{{/items}} if the email needs a fallback.

Layouts, aliases and limits

  • Two template types: Standard and Layout, set when you create the template and not changeable later. A layout holds shared HTML and CSS and must contain the content placeholder {{{ @content }}} exactly once; it can't have a subject.
  • A standard template picks a layout with LayoutTemplate set to the layout's alias.
  • Aliases let your code refer to welcome instead of a numeric ID that differs between servers. Allowed characters are letters, numbers, ., - and _, starting with a letter.
  • Limits: up to 100 templates per server. PUT /templates/push (account token) copies templates with aliases from one server to another, for example staging to production.
  • CSS in <style> blocks is inlined on send unless you pass "InlineCss": false. A <link> tag in the <head> isn't supported and gives ErrorCode 1122.

Send with a template

curl "https://api.postmarkapp.com/email/withTemplate" \
  -X POST \
  -H "Accept: application/json" \
  -H "Content-Type: application/json" \
  -H "X-Postmark-Server-Token: $POSTMARK_SERVER_TOKEN" \
  -d '{
    "From": "[email protected]",
    "To": "[email protected]",
    "TemplateAlias": "receipt",
    "TemplateModel": {
      "first_name": "Ann",
      "items": [{"name": "Pro plan", "quantity": 1}],
      "customer_name": "Example Ltd"
    },
    "MessageStream": "outbound"
  }'

Use TemplateId or TemplateAlias. For many messages, POST /email/batchWithTemplates takes a Messages array of the same objects. The test token POSTMARK_API_TEST doesn't work with templates; send with your server token to [email protected] to see the result in Activity without an inbox.

A Postmark template rendered with sample values, showing subject and body preview
A template filled with sample data.

Validate before you send, and the errors you'll meet

POST /templates/validate parses a subject, HTML and text body, renders them with your TestRenderModel, and returns AllContentIsValid, per-part ValidationErrors (with line and character position) and a SuggestedTemplateModel listing every key the template uses. Run it in CI before pushing template changes.

ErrorCodePostmark's meaning
1101Neither TemplateId nor TemplateAlias given, or the template, alias or layout wasn't found.
1105The server's active-template limit would be exceeded.
1109No template data received.
1120A required field is missing (Name, TextBody/HtmlBody, Subject or TemplateModel).
1121A field is too long.
1122Invalid TemplateType, alias empty, invalid or in use, unparseable body, or a reserved top-level TemplateModel key.
1123Mixing template and non-template fields, such as a layout with a subject.
1131Layout content-placeholder rules not met.

A message can also be accepted and then fail as a bounce of type TemplateRenderingFailed (TypeCode 100010). More codes: Postmark API error codes.

Questions

Does Postmark support if/else in templates?

No. Mustachio has no if/else; use {{#key}}…{{/key}} for when a value is present and {{^key}}…{{/key}} for when it is missing, false, null or empty.

How do I use a template alias in Postmark?

Give the template an Alias (in the editor or with PUT /templates/{id}), then send with "TemplateAlias": "your-alias" instead of TemplateId.

Can I set the subject when sending with a Postmark template?

Not with a Subject field. Put variables in the template's subject, or make the subject {{ subject }} and pass the text in TemplateModel.

Can I test Postmark templates with POSTMARK_API_TEST?

No, the test token can't find your templates. Use your server token and send to [email protected], or render with /templates/validate.

Sources