Template Syntax

Postmark Template Syntax

Postmark Templates use a very simple, yet very powerful, language called Mustachio. Mustachio has its roots with Mustache, but includes a few advanced features that make it great for authoring plain text and html emails.

Here's everything you need to know about the language.

Variable Interpolation:

"Variable Interpolation" is the heart and soul of any templating language.

If you have a template model like this (represented as JSON):

{
"person": {
"first_name": "Andrew"
}
}

And this template:

Hello {{ person.first_name }}

Mustachio will produce this content when you combine the two:

Hello Andrew

If you've used Mustache.js, this should be very familiar. But Mustachio has a few more capabilities that make writing templates a breeze.

Scoping:

Sometimes we'll have more complex models, and so scoping to a particular property will make the markup easier to read:

{{#person}}
Hello {{first_name}}
{{/person}}

Notice that if we're building a section of the template that uses multiple properties from "person", we don't need to keep repeating "person." with each variable interpolation.

Advanced Interpolation:

If we've scoped our template to a property of a model, we may want to "reach up" to a property in the outer scope. For example, let's say we wanted to change the content from the Collection Handling example above to look like this:

Note the
../ in the template, which just means "go up one level" in my template model and look for the property name that follows. You can go up as many levels in your model as needed by including ../ multiple times at the start of your {{ ... }} section.

Advanced Value Handling

Mustachio is permissive about missing values in your models. If you scope to a property that does not exist, Mustachio will skip that section. In this way, you can build templates that do not require if/else logic.

Note that because Road Runner doesn't have a 'position' property, " as a..." is omitted from the output. Also note that we can use
. in the Variable Interpolation to reference the model object in the {{#position}} scoped block.

Inverted Groups (or, how to make Placeholders)

Mustachio will skip anything in a template that reference a value that is null, false, or empty (such as an array that has no elements). However, sometimes it's useful to include content specifically when a value is absent from the model, and, of course, Mustachio supports this using "Inverted Groups":

In the above example, if
years_employed is not specified in our template model, and so therefore, since who knows when? will be rendered.

Inverted Groups are a powerful way to provide placeholder text when information is not available, or not applicable.

Content Safety:

Normally, the information you want to include in a template will be simple scalar values, these values typically lack html markup, and are safe to render to browsers as-is. However, if you accept content from untrusted sources that you wish to embed, it's possible that this content could contain unsafe values that could lead to issues when rendered and served in a browser. This is a common "cross-site scripting" attack, and Mustachio is safe by default: