Optimize Switch for Constants

The FreshMarker Switch Directive is an unusual representative of its kind. Since it has been implemented according to the possibilities of its syntax description in the CongoCC example source code, it is capable of quite unusual working methods. Some special cases could be processed faster with an optimized version. Therefore, the Switch Directive is optimized for constants in this article.

FreshMarker 1.10.0

I am pleased to announce the release of the Java 21 template engine FreshMarker 1.10.0. The version includes some new features and improvements.

List Directive Unrolled

The Partial Template Reduction feature allows FreshMarker templates to be simplified. Part of the model is used to evaluate expressions in directives and interpolations and generate a new template. A template reduced in this way contains fewer dynamic and more static parts and can therefore be evaluated more quickly. Until now, list directives were excluded from the simplification options. This can now change under certain circumstances.

FreshMarker Variable Scope Revised

The blog posts on the FreshMarker template engine pursue several goals. On the one hand, the posts should of course entertain their readers. On the other hand, they also serve me to focus and document my ideas on various aspects of the implementation. One very interesting topic in this post is the management of variables within the Template Engine.

FreshMarker 1.9.0 available

I am pleased to announce the release of FreshMarker 1.9.0. The version includes some new features and improvements. The library is now available on Maven Central. And for all those who like a cheat sheet, there is now one available here.

Checking Types with Build-Ins in FreshMarker

The FreshMarker Template Engine works with an interpolation system that only takes the type of a variable into account at evaluation time. This approach makes the use of variables in FreshMarker very flexible, but occasionally leads to runtime errors. These can be avoided if the type of a variable can be checked beforehand.

FreshMarker Extension API

The FreshMarker Plug-In Mechanism was one of the first features to be realized for the template engine. The idea behind it was simple. With a proper plug-in mechanism, a new feature can be easily realized and integrated and if it fits the core functionality, it stays in FreshMarker. Otherwise it becomes an independent plug-in. This is how FreshMarker File, FreshMarker Money and FreshMarker Random were created. It also created the internal FreshMarker support for Version, Locale, Year, MonthYear, MonthDay, Duration, Period and some other classes.

Compress Whitespace in FreshMarker

Whitespace in a template engine like FreshMarker’s is fundamentally different from the rest of the characters outside of it’s language constructs. A considerable amount of whitespace and line breaks are only used to ensure that the structure of the template remains comprehensible to the author.

FreshMarker – compile it! (4)

A further step on the way to static templates is the automation of their creation. A Maven plugin can be used to convert the FreshMarker template sources into Java source code. Although there are now more modern build tools, Maven is still used in many projects. The first task of the Maven plugin should be the validation of template sources.

Null Aware Built-In Handling 

As soon as the handling of optional values is built into FreshMarker, another interesting feature appears, which could be added with tiny changes. It was implemented in three steps, with the next step only becoming apparent once the previous step had been implemented. The whole thing started with the fact that empty optionals should be processed slightly differently than null values.