Trim Your Templates!

Trimou is a Mustache templating engine implementation written in Java. Helper API inspired by Handlebars.js is also included. The goal is to provide a simple to use and easy to extend templating engine for any Java SE or Java EE application. Trimou is available under the Apache License 2.0.

The most notable advanced features include template caching, template inheritance, Helper API, many extension points and ready-to-use extensions out of the box (CDI, Servlets, PrettyTime, Dropwizard, etc.)

Get the latest version Maven Central

All the artifacts are available in the Maven Central Repository:

<dependency>
  <groupId>org.trimou</groupId>
  <artifactId>trimou-core</artifactId>
  <version>${version.trimou}</version>
</dependency>

Get started!

  1. Hello world!
  2. Template locators and caching
  3. Helpers
  4. Template inheritance

Hello world!

And now for something completely different...

Mustache mustache = MustacheEngineBuilder
                    .newBuilder()
                    .build()
                    .compileMustache("Hello {{this}}!");
System.out.println(mustache.render("world"));
Up

Template locators and caching

Template locators automatically locate the template contents for the given template id. So that it’s not necessary to supply the template contents every time the template is compiled. Moreover, if the template cache is enabled (it is by default) the compiled template is automatically put in the cache and no compilation happens the next time the template is requested.

The usual workflow looks like this:

  1. Build the engine and add template locators as needed
  2. Hold the reference to the MustacheEngine
  3. Whenever the template is needed, obtain a "fresh" instance from the engine

    MustacheEngine engine = MustacheEngineBuilder
        .newBuilder()
        .addTemplateLocator(new FilesystemTemplateLocator(1, "/home/trimou/resources", "txt"))
        .build();
    String output = engine.getMustache("foo").render(null);
Up

Helpers

Helpers are de-facto tags which are able to consume multiple parameters and optional hash map. There are some built-in helpers registered automatically (if, unless, each, etc.). Trimou also provides a lot of useful helpers which are not registered automatically, e.g. ResourceBundleHelper.

Suppose we have the following resource bundle file:

my.message.key=My name is %s!
hello.key.messageformat=Hello {0}!

We can use the ResourceBundleHelper to render messages. First register the helper:

MustacheEngine engine = MustacheEngineBuilder
    .newBuilder()
    .registerHelper("msg", new ResourceBundleHelper("messages"))
    .build();

Now let's create the template:

{{msg "my.message.key" "Martin"}}
{{msg "hello.key.messageformat" "world" format="message"}}

And the output is:

My name is Martin!
Hello world!

Feel free to create your own helper!

Up

Template inheritance

This useful feature is not supported in the spec. Trimou basically follows the way mustache.java implements the template inheritance.

The following template with name "super":

{{$header}}
  The default header
{{/header}}
In between...
{{$content}}
  The default content
{{/content}}

Can be extended in this way:

Hello world!
{{<super}}
  {{$header}}
    My own header
  {{/header}}
  Only extending sections are considered...
{{/super}}
Lalala...

And the result is:

Hello world!
    My own header
In between...
  The default content
Lalala...
Up