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.

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 would reference the TemplateID in the JSON for each email but make the call to the batch API endpoint. 
Below is an example API request body to /email/batch for two emails using templates so you can see what the JSON format looks like.
[{
  "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
}]
	
Last updated August 15th, 2017

Still need some help?

Our customer success team is here to help!