Advanced templating (Handlebars)
Using the Handlebar templating engine to create advanced templates
Personalizing templates with Handlebars
By default, Lob uses a basic templating engine based on Mustache. If you would like to render complex personalizations, use the alternate Handlebars templating engine route through Lob API. Handlebars syntax allows you to populate, evaluate, and iterate using templates and API requests. The biggest advantage of this approach is quickly creating dynamic personalization without making any changes to your codebase. We can use advanced templating to make dynamic tables.
Creating a template
Start with any Handlebars-friendly template. Dynamic variables should be wrapped in double curly braces
{{like_this}}. To the right is an example of a simplehandlebarstemplate.
<html>
Name is: {{user.name}}
Location is: {{user.location}}
</html>To push your template to a Lob environment, use the
/v1/templatesAPI. Creating Handlebars templates via the Dashboard UI is not supported at this time. To specify that our service should use the Handlebars templating engine, you will need passhandlebarsin the template requestenginefield as shown on the right.
POST
[/v1/templates](<https://api.lob.com/v1/templates>)/
{
"description":"Test Template",
"html":"<html>Name is: {{user.name}} <br>
Location is: {{user.location}} </html>",
"engine":"handlebars",
"required_vars": ["user"] //optional
}
Response
{
"id": "tmpl_81ff8f64ce61285",
....
}Template compilation API
You can use this newly created endpoint to test that your Handlebars template compiles as expected. To do that, use the /v1/templates/ endpoint and add your merge variable(s) as a dynamic URL query parameter. You can use this to inspect how your template will display with the merge variables passed in. The response is the same format that Lob will use to render your mailpiece.
Create a mailpiece using Handlebars
Now that your template has been created in a Lob environment, the templating engine has been set to handlebars, and you've tested out your template using the Template Compilation API, you can send a mailpiece request to the /v1/checks/, /v1/postcards/, /v1/letters/ or /v1/self_mailers/ endpoint as you normally would. In the request message body, use the merge_variables field to pass in dynamic values.
Helpers
Built in helpers
The below helpers can be used for many dynamic use cases. Helpers like if blocks can include nested built-in and customer helper conditionals (like if, and, or, eq, etc).
#if
You can use the if helper to conditionally render a block. If its argument returns false, undefined, null, "", 0, or [], Handlebars will not render the block.
When you pass the following input to the above template:
This will produce the result as below:
If the input is an empty JSONObject {}, then author will become undefined and if condition fails, resulting in the output as follow:
When using a block expression, you can specify a template section to run if the expression returns a falsey value. The section, marked by else is called an "else section".
#each
You can iterate over a list using the built-in each helper. Inside the block, you can use this to reference the element being iterated over.
when used with this context:
will result in:
You can use the this expression in any context to reference the current context.
You can optionally provide an else section which will display only when the list is empty.
When looping through items in each, you can optionally reference the current loop index via {{@index}}.
Additionally for object iteration, {{@key}} references the current key name:
The first and last steps of iteration are noted via the @first and @last variables when iterating over an array. When iterating over an object only the @first is available. Nested each blocks may access the iteration variables via depth based paths. To access the parent index, for example, {{@../index}} can be used.
#with
The with-helper allows you to change the evaluation context of template-part.
when used with this context:
will result in:
with can also be used with block parameters to define known references in the current block. The example above can be converted to
Which allows for complex templates to potentially provide clearer code than ../ depthed references allow for.
You can optionally provide an {{else}} section which will display only when the passed value is empty.
#lookup
The lookup helper allows for dynamic parameter resolution using Handlebars variables.
This is useful for resolving values for array indexes.
It can also be used to lookup properties of object based on data from the input. The following is a more complex example that uses lookup in a sub-expression to change the evaluation contextto another object based on a property-value.
Custom helpers
Custom helpers are additional functions that can assist in the formatting of your document. The documentation here is directly pulled from the associated third-party github repo. The helpers below are supported.
Last updated
Was this helpful?

