Martin has posted 9 posts at DZone. You can read more from them at their website. View Full User Profile

Trimou: Yet Another Mustache Templates Implementation Written in Java

06.13.2013
| 2008 views |
  • submit to reddit

The basic question is: do we need yet another templating engine in Java ecosystem? There is a lot of successful tools out there. JSP, Facelets, Velocity, Freemarker, Closure Templates, and much more. I myself asked exactly the same question some time ago when prototyping a hobby web application project with HTML5 on the client side and JAX-RS/CDI/JPA on the server side. I've had the following requirements...

The tool should (in no particular order):

  • have simple yet powerful syntax
  • be able to handle various template data types (POJO, Map, array, etc.) without the need of wrapping/manipulating it beforehand
  • be universal - ie. not tied to a specific technology, unlike Facelets/JSF
  • support template inheritance (similar to Facelets or Django); IMO this is crucial for maintenance and productivity
  • be as much as possible resistant to bad practices; writing scriptlets a ala JSP should not be possible... never ever!
  • be performant
  • be extensible - no hacks needed, stable API and SPI provided

I found out that none of those "proven" tools fits my needs without tradeoffs. Don't get me wrong. All of those tools mentioned above do satisfy most of the requirements. But I was not under any pressure so I kept on searching. Finally I found Mustache templates. Mustache templates have really nice and almost "logic-less" syntax. Currently there are many implementations, including JavaScript, Ruby, Scala and even Java. The great thing is most of those implementations are spec compliant (or provide some compatibility mode at least). In fact the spec is just a set of tests (a kind of Java TCK) each implementation should pass. Of course most of the implementations do also add some extra features (extensions). Existing Java implementations worked well but did not entirely match all my needs. So in the end I realized that it shouldn't be that hard to create a new implementation that fulfils all of the given requirements. And this his how Trimou was born - yet another open source Mustache implementation written in Java. The goal: try to satisfy all of the requirements mentioned above. If you're interested you can find more info at http://trimou.org.

0
Average: 4 (1 vote)
Published at DZone with permission of its author, Martin Kouba.

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)