Back To Blogs
Iain Lindsay 22nd Jan 2021

Advanced string templates in Mendix with {{ Handlebars.java }}

When working with Mendix, there are a number of scenarios where a dynamic string template is required. A common use is managing email templates. The standard way to accommodate dynamic values is through the use of tokens, mapped (via model reflection) to attributes or microflows that allow for a replacement of data at runtime. This approach works great in most scenarios but presents a few limitations when working with:

  • repeating sections (e.g. tables of information)
  • conditional sections
  • data formatting of token values

Working around these limitations is possible with the use of microflows that encapsulate the complexity e.g. building an HTML table that is stored in an attribute with a corresponding token. However, this abstracts some of the content and logic away from the template, which could have an impact on maintainability, and potentially forces a deployment for a minor content change. This blog introduces a different method, using Handlebars.java, that allows you to define semantic templates and addresses the limitations described above.

{{ Handlebars.java }}

Handlebars.java (https://github.com/jknack/handlebars.java) is a java implementation of Mustache (http://mustache.github.io/mustache.5.html), providing a multi purpose template engine that allows you to define semantic templates.

Templates can be defined using syntax for the three most used implementations:

A template using this syntax consists of a series of tags, represented by a value contained within double curly brackets, e.g. {{tagName}}. A tag can be a variable name, can change the evaluation context (i.e. to access associated data) and can also apply helper functions to control things like conditionality, data transformation and data formatting.

AqHandlebars

We have created AqHandlebars, a simple Mendix module built on top of Handlebars.java that allows Mendix developers to use this template syntax in their app. Data is provided to the template as a JSON object or a JSON array and the module handles the conversion of your template, returning a merged string. So, given a domain model that looks like this (left-hand image), we can represent data for an invoice in JSON as follows (right-hand image):

Building a template – standard functionality

We can access a root level property by name as follows:
{{invoiceNumber}}

To access a nested property, we can either access it using dot-notation:
{{customer.customerName}}

Or (useful if we want to access multiple properties) we can change the context before accessing the properties by name:

Adding repeating sections is the same syntax as changing the context:

There are three conditions available as standard, all three are only supported with boolean values.

An if statement:

An if/else statement:

An unless statement (checks for a false value):

Re-using template snippets with partials

If there are sections of dynamic text that you want to use across multiple templates then you can define and reference partial templates. So, a partial template with a name of ‘invoices’ and the following content:

Can then be referenced in my templates as follows:
{{>invoices}}

Extending the syntax through custom helpers

The standard functionality already addresses some of the limitations described at the top of this article, however, we can take this further by extending the functionality supported by a template through the creation of custom helpers. Custom helpers can be used for lots of different things, such as:

  • checking value equality (extending conditional functionality)
  • data formatting (formatting of dates, decimals etc.)
  • data transformation (replacing values with html elements, such as transforming a link to an anchor tag)

To define a custom helper you need to create a Java class that implements the Helper interface from com.github.jknack.handlebars. An example custom helper that transforms a value into an anchor tag is shown on the left. AqHandlebars provides an additional interface, ICustomHelperRegister, that allows a Mendix application to register these helpers for use in templates. An example implementation of this interface, using our custom helper is shown to the right:

The final step is to register this implementation via the Mendix front end. And now, we can use this in our templates:
{{anchor customer.website "tache"}}

Which produces this:
<a href='https://www.handlebarclub.co.uk/'>tache</a>

Conclusion

Handlebars provides a powerful, semantic way to build dynamic string templates within your Mendix application. The limitations with tokens described in the introduction can be addressed and the module is fully extensible to support whatever logic is required by your applications.

Tokens and Handlebars syntax are not, however, mutually exclusive. They can be used side by side so there is no need to rewrite existing templates; they can be adapted over time as required to support more complex scenarios. Download the module today from app store and get templating!

Related Blogs


Mendix low-code fundamentals – applying application security

Looking to ensure secure application development? Episode 3 of our Mendix Fundamentals Series describes how security rules can be applied and helps you understand the basic security principles within the Mendix low-code application development environment.

Find Out More

Mendix low-code fundamentals – building the pages of your application

Episode 2 features Mendix developer Luca Santese demonstrating how to build Pages and create an engaging front-end for your Mendix low-code application!

Find Out More

Mendix low-code fundamentals – creating the domain model

Our new Mendix Fundamentals Series contains explanations and walkthroughs of some of the key concepts used in the Mendix low-code development platform. Across the four-part series, AuraQ’s team of experienced Mendix developers will cover the essential features required to build an effective Mendix application including the domain model, pages, security and microflows.

Find Out More

Leveraging machine learning capabilities in application development

In recent years, the digital sector has been transformed by artificial intelligence (AI). With tools such as ChatGPT and DALL-E, public access to AI resources is at an all-time high.

Find Out More

Git integration in Mendix

Mendix has chosen Git as their standard for version control going forwards. Explore some of the differences between using Git and SVN and walk through how developers use Git version control when creating both new applications and when converting existing Mendix applications.

Find Out More
Drag