역시 시퀀스가 비어 있다면 에러를 반환할 것이다.
3. seq_contains
설명 : FreeMarker 2.3.1 버젼이상에서 동작한다. 이하에서는 동작하지 않음 주의 !
시퀀스내에 특정 값이 존재하는지 검사한다. 만약 존재한다면 참을 반환한다.
주의할 것은 만약 시퀀스에 들어있는 값의 형식이 찾고자 하는 형식과 다르다면 false를 반환함을 주의해야한다.
(즉, 반드시 동일한 타입끼리만 찾을수 있다.)
<#assign x = ["red", 16, "blue", "cyan"]>
"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")}
사용법 : sequence?seq_index_of("찾을값")
sequence?seq_index_of("찾을값", 찾을 위치)
설명 : 시퀀스에서 매칭되는 값이 존재하면 해당 값의 인덱스를 반환한다.
매칭되는 값이 존재하지 않을경우 -1을 반환한다.
역시 이것도 동일한 타입만을 검색할 수 있다. 타입이 다르면 찾지 못했을경우 -1을 반환할 것이다.
두번째 사용법은 찾을위치를 넣을수 있는데 음수이면 0으로 처리되어 첫번째 내용가 같아진다.
<#assign colors = ["red", "green", "blue"]>
${colors?seq_index_of("blue")}
${colors?seq_index_of("red")}
${colors?seq_index_of("purple")}
<#assign names = ["Joe", "Fred", "Joe", "Susan"]>
No 2nd param: ${names?seq_index_of("Joe")}
-2: ${names?seq_index_of("Joe", -2)}
-1: ${names?seq_index_of("Joe", -1)}
0: ${names?seq_index_of("Joe", 0)}
1: ${names?seq_index_of("Joe", 1)}
2: ${names?seq_index_of("Joe", 2)}
3: ${names?seq_index_of("Joe", 3)}
4: ${names?seq_index_of("Joe", 4)}
<#assign names = ["Joe", "Fred", "Joe", "Susan"]>
No 2nd param: ${names?seq_last_index_of("Joe")}
-2: ${names?seq_last_index_of("Joe", -2)}
-1: ${names?seq_last_index_of("Joe", -1)}
0: ${names?seq_last_index_of("Joe", 0)}
1: ${names?seq_last_index_of("Joe", 1)}
2: ${names?seq_last_index_of("Joe", 2)}
3: ${names?seq_last_index_of("Joe", 3)}
4: ${names?seq_last_index_of("Joe", 4)}
8. sort
설명 : 시퀀스의 내용이 소트된다. (어센딩으로 소팅됨)
단 이것은 시퀀스의 내용이 모두 동일한 타입인경우에 (string, number)만 적용된다.
또한 이것은 대소문자를 따지지 않는다.
{"name":"whale", "weight":2000},
{"name":"Barbara", "weight":53},
{"name":"zeppelin", "weight":-200},
{"name":"aardvark", "weight":30},
{"name":"beetroot", "weight":0.3}
Order by name:
<#list ls?sort_by("name") as i>
- ${i.name}: ${i.weight}
</#list>
Order by weight:
<#list ls?sort_by("weight") as i>
- ${i.name}: ${i.weight}
</#list>
<#assign members = [
{"name": {"first": "Joe", "last": "Smith"}, "age": 40},
{"name": {"first": "Fred", "last": "Crooger"}, "age": 35},
{"name": {"first": "Amanda", "last": "Fox"}, "age": 25}]>
Sorted by name.last:
<#list members?sort_by(['name', 'last']) as m>
- ${m.name.last}, ${m.name.first}: ${m.age} years old
</#list>
사용법 : sequence?chunk(3)
sequence?chunk(3, '-')
설명 : 분할된 시퀀스의 단어들을 주어진 숫자개수만큼 병합해서, 새로운 시퀀스를 만들어 낸다.
두번째 파라미터의 경우 숫자만큼 구성되지 않은 단어는 - 로 채워 넣기한다.
<#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']>
<#list seq?chunk(4) as row>
<#list row as cell>${cell} </#list>
</#list>
<#list seq?chunk(4, '-') as row>
<#list row as cell>${cell} </#list>
</#list>