Monday, October 6, 2014

Java Template Engines Revisited Part 1

Over the past week, I spent some time looking at Java based template engines. Typically I need templating support for two areas:

  • View Templates (For rendering views in your browser)
  • Email Templates - with support for both HTML and Text emails

For email templates I had used the usual suspects such as Velocity and Freemarker in the past but both feel a tad heavy and old these days - Velocity's last release was in 2010! Eventually I settled for a simpler option a while back: StringTemplate, which as a library worked fairly okay.

As I had done some client-side templating using Mustache and Handlebars, I was intrigued in seeing Java implementations for both:


The nice thing about Mustache is that implementations are available for almost any programming language imaginable, which could be nice in case you have the need to maintain browser-bound and backend (Java) templates or in case you have multiple Java and non-Java application with templating needs.

For now I have chosen mustache.java. Looks like it is heavily used at Twitter. Depending on how willing you are towards enduring any type of logic in your templates, you may also want to check out Handlebars and the corresponding Java implementation. It is basically a super-set of Mustache, providing additional built-in helpers.

Lastly, for both Mustache and Handlebars there is support available for Spring MVC.


I have not used either support for Spring MVC, yet, though. In case you have used any of the mentioned options, please leave feedback to this blog.


3 comments:

James Moger said...

I have two additional engines for you to checkout.

Trimou: server-side-friendly Mustache

Pebble: borrows ideas from several engines

Both of them are pretty nice.

Alex Sherwin said...

There's a glaring, missing hole here... http://www.thymeleaf.org/

Gunnar Hillert said...

That's my next blog entry :-) At the same time I was also working on a template using Thymeleaf for Spring XD. Will try to get that blog post out later this week. Good point though - should have at least mentioned it. Another bigger one to consider is Scalate for sure.

I just remembered, I came across the following presentation on Slideshare that also attaches some performance numbers:

http://www.slideshare.net/jreijn/comparing-templateenginesjvm