FreshMarker – What happened in between

Some FreshMarker additions are too small to present individually in a blog post. For this reason, there will be a brief overview of the latest changes to the library from time to time. Up to the current version 1.6.0, the following changes and extensions have been made in recent weeks.

The use of the library has changed slightly. Previously, the templates were created via the Configuration class, as with Freemarker. Several Configuration#getTemplate methods were previously available for this purpose. However, the Configuration class became larger and larger over time, so that it was functionally split. Most of the configuration is still carried out by the Configuration, but the creation has been outsourced to the TemplateBuilder class.

Configuration configuration = new Configuration();
TemplateBuilder templateBuilder = configuration.builder().withLocale(Locale.GERMANY);
Template template templateBuilder.getTemplate(Path.of("templates/email.ftl"));

An instance of TemplateBuilder is created via the Configuration#build method. Template instances can then be created via the TemplateBuilder#getTemplate methods. Some configurations for templates (for Locale, ZoneId and OutputFormat) have also been moved from the Configuration class to TemplateBuilder.

Double, Float, Long, Integer, Short and Byte were previously supported for numerical types. For the sake of completeness, BigDecimal and BigInteger have now also been added.

Various new Build-Ins have been added. As a counterpart to the classic Build-In c (computer language), there is now also h (human language). This outputs some values of selected data types in words. For example, dates can be output as the day before yesterday, yesterday, today, tomorrow and the day after tomorrow instead of the usual date format.

For string interplations, the new Built-In slugify is available to create slugs from texts. The join Built-In is available for sequences to enable simple concatenations without the List Directives.

<#list marxs as marx with looper><#if looper?is_first><#elseif looper?has_next>, <#else> and </#if>${marx}</#list>

The above template creates the output Chico, Harpo, Groucho, Gummo and Zeppo for the Marx Brothers. The template is quite confusing, but can now be replaced by the following interpolation.

${user?join(' and ', ', '}

These are the current additions to the FreshMarker library until the beginning of October. But there will certainly be something new to report soon.

    Leave a Comment