Search code examples
freemarker

How to print any variable in FreeMarker?


I have a Map<String, Object> which contains Strings, Doubles, Integers, Booleans and null (missing) as values. I want to print out the value in a FreeMarker template.

When I use ${data[field]!?html} it works for all other variables, but crashes on Boolean with:

freemarker.core._MiscTemplateException: Can't convert boolean to string automatically, because the "boolean_format" setting was "true,false", which is the legacy default computer-language format, and hence isn't accepted.

The application is an administration UI, where I don't that much care about the exact formatting, but I'd prefer booleans to use the standard true and false. (The boolean_format flag controls standard formatting of booleans, but using true and false seems to be explicitly prohibited for some mind-boggling reason.)

What's the best way to work around the FreeMarker prohibitation?


Solution

  • The way to achieve this is to use the ?string method: ${data[field]!?string?html}

    This was mentioned only in the Configurable JavaDoc, and the ?string method is deprecated. I hope it's not going away anytime soone.