在 freemarker 模板中,我想将布尔变量扩展为这样的字符串:
<#assign booleanVar = "test".isEmpty() />
state: ${booleanVar} <#-- this throws an exception! -->
这就是我想要得到的输出:
state: false
我现在发现达到这个目标的唯一方法是:
state: <#if booleanVar>true<#else>false</#if>
有更简单的方法吗?
在 freemarker 模板中,我想将布尔变量扩展为这样的字符串:
<#assign booleanVar = "test".isEmpty() />
state: ${booleanVar} <#-- this throws an exception! -->
这就是我想要得到的输出:
state: false
我现在发现达到这个目标的唯一方法是:
state: <#if booleanVar>true<#else>false</#if>
有更简单的方法吗?
从 FreeMarker 2.3.20 开始,如果您想打印真/假(因为您正在生成 JavaScript 等),请编写${booleanVar?c}
(?c
用于“计算机格式”,也用于数字)。${booleanVar?string}
这样做很危险,因为有人可以将boolean_format
设置设置为yes,no
……(顺便说一句,在这种情况下,${booleanVar}
在 2.3.20 中也可以工作,你得到yes
and no
。)
See: http://freemarker.org/docs/ref_builtins_boolean.html#ref_builtin_c_boolean