Freemarker判断序列中是否包含某个元素

在Freemarker中,如果要判断序列中是否包含某个指定的元素,可以使用freemarker的内建函数seq_contains。

注:seq_contains这个内建函数从FreeMarker 2.3.1 版本开始可用。而在2.3 版本中不存在

使用示例:

<#--声明一个序列,包含若干个元素-->
<#assign x = ["red", 16, "blue", "cyan"]>
<#--使用seq_contains判断序列中的元素是否存在-->
"blue": ${x?seq_contains("blue")?string("yes", "no")}
"yellow": ${x?seq_contains("yellow")?string("yes", "no")}
16: ${x?seq_contains(16)?string("yes", "no")}
"16": ${x?seq_contains("16")?string("yes", "no")}

输出结果:

"blue": yes
"yellow": no
16: yes
"16": no

附:seq_前缀在这个内建函数中是需要的,用来和contains 区分开。contains函数用来在字符串中查找子串(因为变量可以同时当作字符串和序列)。

猜你喜欢

转载自chenzhou123520.iteye.com/blog/1976874