For month now, I’ve been in quest of a Java template engine for one of my projects. My first MVC (or MTV) project many years ago was build with Django. The build-in template engine is just beautiful. PHP has TWIG and Javascript has Moustache, Handlebars and Jade.

So what do we have in Java Land? Velocity, Freemarker and Thymeleaf.

I’ve used Velocity for years now and it’s… ok. But it’s not templating, it’s writing Java surrounded by HTML. I’ve seen people loading Objects by reflection and writing to the Database. Yes. In a template. This is not only messy, but also expensive for your company (or customer) who has to pay the frustrated developers trying for hours, days or weeks to find a bug, because someone “accidentally” called a setter in a template.

Freemarker must be similar to Velocity and Thymeleaf… sorry, but this just hurts my eyes: <p hello:sayto="World">Hi ya!</p>.

I’m sure there are good template engines out there. I’ve stumbled upon Pebble and Jtwig. These look very promising and feature complete. But they are not proven in the wild.

So what to do? I’ve decided - and this could change - but at least for now, I’ve decided to use Handlebars. Handlebars for Java. I figured Handlebars is popular in Javascript and Handlebars is simple. Simplicity was the key, I guess. It is build on Moustache which is lightweight and logic-less. In addition, it has support for helpers. And that’s it. If you want to extend it with functionality, just write a helper. Even ‘if’ and ‘each’ are just helpers. I love things that are lightweight, but have a powerful extensions mechanism.

Writing templates is also a lot of work and you don’t want to rewrite everything just because a template engine died. But if it’s simple and lightweight, you can always maintain it yourself. I guess. And because it was developed for Javascript, I’m sure I could somehow reuse the server-side templates on the client.

So that’s the plan. We’ll see how it goes…