Templates FAQ
How do I localize my templates for different languages?
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>
{ "english" : true, "name" : "Dave" }
<html> <body> Good morning, Dave </body> </html>
How do I use an ‘If’ statement?
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?
Dear {{#name}}{{.}}{{/name}}{{^name}}{{../job_title}}{{/name}}
- Render name if it is"truthy" or,
- Render job_title if it is not present:
{ "name" : "Alice", "job_title" : "CEO" }
Dear Alice
{ "job_title" : "CEO" }
Dear CEO
Model:
{ "name" : null, "job_title" : "CEO" }
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:
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?
Can I send a batch of emails with the API using a Template?
[{ "TemplateId": 123456, "TemplateModel": { "user_name":"John Smith", "company": { "name":"ACME" } }, "InlineCss": true, "From":"sender@example.com", "To":"recipient@example.com", "Cc": null, "Bcc": null, "Tag": null, "ReplyTo": null, "TrackOpens": true, "Attachments": null }, { "TemplateId": 123458, "TemplateModel": { "user_name":"Guy F.", "company": { "name":"Diners, Drive-Ins, and Dives" } }, "InlineCss": true, "From":"sender@example.com", "To":"recipient@example.com", "Cc": null, "Bcc": null, "Tag": null, "ReplyTo": null, "TrackOpens": true, "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" }