There are many situations where content needs to be created automatically. The most common case is probably the dynamic generation of web pages, for which template engines are a popular choice. Some template engines take a logic-less approach, by denying or discouraging the use of logic in templates. Examples of logic-less template systems are Mustache and Handlebars, for which Java implementations are also available: Mustache.java and Handlebars.java.

Logic-less templates seem to polarize opinions. Proponents see them as the best way to achieve a clear separation of concerns in MVC architectures. Opponents argue that forcing the controller to do data-massaging before sending the data to the UI is actually a bad thing, because it forces the controller to be aware of the presentation logic.

I will not take a side in this debate, because I have very little experience in developing web applications. However, template engines are also useful for other purposes and I argue that there are cases, especially in non-MVC settings, where using logic in templates is legitimate. Nonetheless, putting too much logic in a template is still a bad idea.

I used recently Handlebars.java for generating source code and I needed to add some logic to my templates. Handlebars.java allows you to do this through the use of helpers, therefore I wrote the Handlebars.java Helpers library, which provides a series of helpers for common use cases. Most of them are basic helpers that can be used as subexpressions in the built-in block helpers of Handlebars.java. This lets you write the template logic in a fluent way.

Example

Given the following YAML model:

birthYear: 1997

and the following template:

{{def 'fifteenYear' (math birthYear '+' 15)}}
{{#ifb (or
    (and
        (compare (math fifteenYear '%' 4) '==' 0)
        (compare (math fifteenYear '%' 100) '!=' 0)
    )
    (compare (math fifteenYear '%' 400) '==' 0)
   )
}}
Your fifteenth anniversary was in a leap year!
{{else}}
Your fifteenth anniversary was in a non-leap year!
{{/ifb}}

The resulting text will be:

Your fifteenth anniversary was in a leap year!

 
If you use Handlebars.java, you may find this library useful, so read its documentation and give it a try!