Expand a boolean variable to the string “true” or “false”
-
19-09-2019 - |
Question
In a freemarker template I want to expand a boolean variable to a string like that:
<#assign booleanVar = "test".isEmpty() />
state: ${booleanVar} <#-- this throws an exception! -->
This is what I want to get as output:
state: false
The only way I found to reach this goal by now is:
state: <#if booleanVar>true<#else>false</#if>
Is there a easier way to do it?
Solution
booleanVar?string("true", "false")
Although true/false is default, so
booleanVar?string
should work fine.
OTHER TIPS
Starting from FreeMarker 2.3.20, if you want to print true/false (because you are generating JavaScript or such), write ${booleanVar?c}
(?c
for "computer format", also used for numbers). ${booleanVar?string}
is dangerous for that, since somebody can set the boolean_format
setting to yes,no
or something... (BTW, in that case ${booleanVar}
will work too in 2.3.20, and you get yes
and no
.)
See: http://freemarker.org/docs/ref_builtins_boolean.html#ref_builtin_c_boolean