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

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

- "blue": yes
- "yellow": no
- 16: yes
- "16": no
附:seq_前綴在這個(gè)內(nèi)建函數(shù)中是需要的,用來和contains 區(qū)分開。contains函數(shù)用來在字符串中查找子串(因?yàn)樽兞靠梢酝瑫r(shí)當(dāng)作字符串和序列)。