9

在 freemarker 模板中,我想将布尔变量扩展为这样的字符串:

<#assign booleanVar = "test".isEmpty() />
state: ${booleanVar} <#-- this throws an exception! -->

这就是我想要得到的输出:

state: false

我现在发现达到这个目标的唯一方法是:

state: <#if booleanVar>true<#else>false</#if>

有更简单的方法吗?

4

2 回答 2

23
booleanVar?string("true", "false")

虽然true/false 是默认值,所以

booleanVar?string

应该可以正常工作。

于 2009-10-02T17:59:02.460 回答
5

从 FreeMarker 2.3.20 开始,如果您想打印真/假(因为您正在生成 JavaScript 等),请编写${booleanVar?c}?c用于“计算机格式”,也用于数字)。${booleanVar?string}这样做很危险,因为有人可以将boolean_format设置设置为yes,no……(顺便说一句,在这种情况下,${booleanVar}在 2.3.20 中也可以工作,你得到yesand no。)

See: http://freemarker.org/docs/ref_builtins_boolean.html#ref_builtin_c_boolean

于 2013-06-28T11:08:25.743 回答