Skip to content

Syntactic Sugar

revusky edited this page Nov 7, 2023 · 1 revision

This page describes some extra, more or less minor syntactic sugar that the newer FreeMarker version has.

Assertions

FreeMarker 3 has assertions that are broadly similar to what exist, for example, in Java. You can write:

#assert foo > bar : "Foo is supposed to be greater than bar here!"

(The above uses the newer terse syntax, of course.)

That, actually, is just syntactic sugar. In previous incarnations of FreeMarker, the equivalent could have been expressed perfectly well as:

[#if foo <= bar]
   [#stop "Foo is supposed to be greater than bar here!"]
[/#if]

In any case, assertions can be useful. In a development phase, you can pepper your template code with these assertions and have these things that alert you when something is amiss.

Ternary operator

FreeMarker3 has a ternary operator that is broadly similar to what you have in Java. In Java you can write:

  x = someCondition ? y : z;

In FreeMarker, you would write:

  #set x = someCondition ?: y : z

Actually, the only real difference is that we use ?: instead of just a lone ? since the latter is used for built-ins.