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函數用來在字符串中查找子串(因爲變量可以同時當作字符串和序列)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章