Templates FAQ

How do I localize my templates for different languages?

Although Mustachio doesn't include explicit support for localizing templates, in many cases, only minor adjustments are needed in the content of a template, so it's simple to include the variations inline.

For example, let's create a basic template that can use different language for Spanish, or English languages:
<html>
   <body>
      {{#english}}Good morning, {{../name}}{{/english}}
      {{#spanish}}Buenos días, {{../name}}{{/spanish}}
   </body>
</html>
Given a model like this:
{
   "english" : true,
   "name" : "Dave"
}
The template would produce the following result:
<html>
   <body>
     Good morning, Dave
   </body>
</html>
	
Note that the presence of the "spanish" property is not necessary.
Many templates require only small modifications to their content for them to be properly localized, but this same technique could be used to create a large block of localized content for each target language. Additionally, using our advanced scoping features ../, it's possible to navigate up and around the template model for more complex scenarios.

How do I use an ‘If’ statement?

You might have noticed, Mustachio doesn't have any if-else syntax built in. This was done deliberately in Mustache, for some good reasons, and we tried to keep Mustachio as close to Mustache as we could.
Does this mean you can't show or hide content conditionally?

Of course not! Mustache and Mustachio allow you a great amount of flexibility, but the flow control is more"declarative" than"imperative." So what does this mean?
Testing for Existence
The key difference between the way we'd choose to conditionally execute application code, and the way we choose what to render is based on whether a value is present in a model, or absent ("truthy" or"falsey"). So, let's take a simple case and show how you can both render content when a value is present, and when it is absent:
An Example Template:
Dear {{#name}}{{.}}{{/name}}{{^name}}{{../job_title}}{{/name}}
This template will either:
  1. Render name if it is"truthy" or,
  2. Render job_title if it is not present:
(Make special note of the {{ . }} syntax to reference name and {{ ../job_title }} to " move up" a level in the scope.)
Now, let's have a look at what the output is for a few different models:
Here's an example where name is present:
Model:
{
  "name" : "Alice",
  "job_title" : "CEO"
}
Result:
Dear Alice
Here's an example where name is absent:
Model:
{
  "job_title" : "CEO"
}
Result:
Dear CEO
name may also be false or null and will produce the same result as when absent:

Model:
{
  "name" : null,
  "job_title" : "CEO"
}
	
Result:
Dear CEO
	

It may seem a bit odd to author templates without explicitly including conditionals, but hopefully it's clear that one can achieve the same results with "logic-less templates," while requiring remembering less syntax.

How do I set the Subject at the time of sending when using a Template?

The way to do this is to add variables to your Template subject.

With Templates, you can either set a default subject that is the same for every email sent using that Template or you can also use Template variables to make the subject dynamic. You cannot set the "Subject" field in your JSON to send the email directly, like you can when sending a message that does not use a Template.

For a dynamic subject example, let's say you had your subject set like this in the Template:

Screen-Shot-2018-02-20-at-1-18-15-PM.png
You could then pass in a product_name and name value in your Template model at the time of sending to have those values reflected in the subject of the email.

You could even set your Subject for the Template to just {{ subject }} and then populate the full subject in your Template model.

The key thing to remember is that the Subject field for the Template can be set up to use Template variables, which you then populate in your Template model when sending, like you would for your other variables in the Template.

Why am I getting {"ErrorCode": 1122,"Message":"The'HtmlBody' could not be parsed." } when creating a Template with the API?

Check if you have a <link> tag in your <head> in the Template HTML. This is not supported and will cause the error.

Can I send a batch of emails with the API using a Template?

Yes! You can send an array of messages to send that use templates to the batch endpoint.
Below is an example API request body to /email/batchWithTemplates for two emails using templates, so you can see what the JSON format looks like.
{
  "Messages": [
    {
      "From": "sender@example.com",
      "To": "receiver@example.com",
      "Cc": "copied@example.com",
      "Bcc": "blank-copied@example.com",
      "Tag": "Invitation",
      "ReplyTo": "reply@example.com",
      "TemplateAlias": "invitation-letter",
      "TemplateModel": {
        "fizz": "buzz"
      },
      "Headers": [
        {
          "Name": "CUSTOM-HEADER",
          "Value": "value"
        }
      ],
      "TrackOpens": true,
      "TrackLinks": "None",
      "Attachments": [
        {
          "Name": "readme.txt",
          "Content": "dGVzdCBjb250ZW50",
          "ContentType": "text/plain"
        },
        {
          "Name": "report.pdf",
          "Content": "dGVzdCBjb250ZW50",
          "ContentType": "application/octet-stream"
        },
        {
          "Name": "image.jpg",
          "ContentID": "cid:image.jpg",
          "Content": "dGVzdCBjb250ZW50",
          "ContentType": "image/jpeg"
        }
      ]
    },
    {
      "From": "sender@example.com",
      "To": "receiver2@example.com",
      "Cc": "copied2@example.com",
      "Bcc": "blank-copied2@example.com",
      "Tag": "welcome",
      "ReplyTo": "reply2@example.com",
      "TemplateAlias": "welcome-email",
      "TemplateModel": {
        "fizz": "buzz"
      },
      "Headers": [
        {
          "Name": "CUSTOM-HEADER",
          "Value": "value"
        }
      ],
      "TrackOpens": true,
      "TrackLinks": "None",
      "Attachments":  null
    }
  ]
}
	

Can I use POSTMARK_API_TEST to test sending with a Template?

This test token (POSTMARK_API_TEST) does not work with the API endpoint for sending using a Template. Since the POSTMARK_API_TEST token is not associated with a particular account or server, your unique Template IDs cannot be matched to the test token. This test token can only be used for testing sending a single email not using a Template or a batch of emails.

If you want to test a Template and do not need to actually receive the sent test email, you can send using your Server API Token to test@blackhole.postmarkapp.com. We will then show the send in your Activity but you do not receive an email for the test in an inbox.

How do I use a template alias?

If you wish to reference a template by an alias instead of its Template ID, you can set an alias that you then use in your API call to send an email with the template. To set an alias, edit the template using a PUT call to  https://api.postmarkapp.com/templates/{templateID} with the alias you want to set included in the body:

{
	"Name": "Template Name",
	"Alias":"my-alias"
}

Once you have your alias set, you can now use it to send an email with that template. You will still send the request to send to the same endpoint but instead of including a "TemplateId" field for identifying the template, you will use your alias for the template in a "TemplateAlias" field, like this:

{
  "TemplateAlias": "my-alias",
  "TemplateModel": {
    "user_name": "John Smith",
    "company": {
      "name": "ACME"
    }
  },
  "From": "sender@example.com",
  "To": "receiver@example.com"
}
Last updated August 10th, 2018

Still need some help?

Our customer success team has your back!