Prospects for 2.3.0

After the release is before the release, so there are already some new features in the queue for FreshMarker 2.3.0. Since it is not always worth writing a separate article for a feature, here is a report on the new features planned so far.

Escapes in String Literals

String literals have been treated very neglected since the first FreshMarker version. Due to the adopted grammar, string literals worked in single and double quotation marks and if one of the two characters was needed in the literal, the other was simply used as a delimiter.

Furthermore, escape sequences are only necessary if the source code is not written in Unicode or if the string contains line breaks. However, it is now very difficult to find tools that cannot process Unicode, so template sources can always be written in Unicode. For template sources embedded in Java code, the Java escape mechanism can also be used. A final, unpleasant reason for the late introduction of escape sequences is that string literals in FreshMarker can contain line breaks.

${'Dies ist
ein Test'}

In fact, this fragment contains a correct interpolation that outputs the text with the line break. In future, this will be objected to with a warning but will probably even be prohibited.

The next Freshmarker version knows the escape sequence \uxxxx with which any Unicode characters can be provided by their hex code. There are also the shortcuts \", \', \\, \n, \r, \t, \b, \f known from Java.

Character Literals

Since Freshmarker 1.11.0, the template engine also supports the character data type. Previously there were no literals for this because the usual representation 'c' is ambiguous. It can either be a character literal or a string literal. With the new version and the feature SystemFeature.CHARACTER_LITERAL, string literals with one character are interpreted as character literals. Escape sequences are also taken into account, so that the literal '\2714' is not a string but the character .

Environment Variables

FreshMarker has a new built-in variable .env, which can be used to access variables from the system environment and the system properties within the template. If you want to display the Java version used to generate the template output, you can now do this with ${.env['java.version']}.

New String Built-Ins

The string type has been given six new build-ins. No wonder with a system that generates texts.

The first three match, find and split use regular expressions. The built-in match checks whether the entire text matches the pattern, find checks whether part of the text matches the pattern and split splits the text into a sequence.

${text?split('\s+')?reverse?join('-')}

The above interpolation produces the output Dog-lazy-the-over-jumps-Fox-brown-quick-The for the value The quick brown Fox jumps over the lazy Dog in the variable text.

The other three built-in are datetime, date and time. This generates a value of type LocalDateTime, LocaleDate or a LocalTime from a string.

Beans and Records as Model

Until now, the data models for Freshmarker were always maps with keys of type string. This is necessary because the top-level variables are addressed via a name. The next version of FreshMarker now also supports beans and records as model. This means that a dummy map with only one entry no longer needs to be created as a model if the data model is represented by a bean.

These are the new features implemented so far for FreshMarker 2.3.0. Let’s wait and see what else happens before the release.

Leave a Comment