在Freemarker中,如果要判斷序列中是否包含某個指定的元素,可以使用freemarker的內建函數seq_contains。
注:seq_contains這個內建函數從FreeMarker 2.3.1 版本開始可用。而在2.3 版本中不存在。
使用示例:
Freemarker代碼

- <#--聲明一個序列,包含若干個元素-->
- <#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")}
輸出結果:
Freemarker代碼

- "blue": yes
- "yellow": no
- 16: yes
- "16": no
附:seq_前綴在這個內建函數中是需要的,用來和contains 區分開。contains函數用來在字符串中查找子串(因為變量可以同時當作字符串和序列)。