본문 바로가기

카테고리 없음

FreeMarker sequence 이용하기


1. first
설명 : 시퀀스의 첫번째 값을 가져온다.
         만약 시퀀스가 비어 있다면 에러를 반환할 것이다.

2. last
설명 : 시퀀스의 마지막 값을 가져온다.
         역시 시퀀스가 비어 있다면 에러를 반환할 것이다.

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")}  

The output will be:

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


4. seq_index_of
사용법 : 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")}  

will output this:

2
0
-1  


찾을 위치가 들어간 사용예 :

<#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)}  

will output this:

No 2nd param: 0
-2: 0
-1: 0
 0: 0
 1: 2
 2: 2
 3: -1
 4: -1  


5. seq_last_index_of
설명 : seq_index_of와 동일하나 뒤에서 부터 찾는다.

예제 :
<#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)}  

will output this:

No 2nd param: 2
-2: -1
-1: -1
 0: 0
 1: 0
 2: 2
 3: 2
 4: 2  


6. reverse
설명 : 시퀀스를 역순으로 정렬한다.

7. size
설명 : 시퀀스내의 요소 개수를 반환한다.
         마지막 인덱스는 sequence?size - 1과 같다.

8. sort
설명 : 시퀀스의 내용이 소트된다. (어센딩으로 소팅됨)
         단 이것은 시퀀스의 내용이 모두 동일한 타입인경우에 (string, number)만 적용된다.
         또한 이것은 대소문자를 따지지 않는다.

예제 :
<#assign ls = ["whale", "Barbara", "zeppelin", "aardvark", "beetroot"]?sort>
<#list ls as i>${i} </#list>  

will print (with US locale at least):

aardvark Barbara beetroot whale zeppelin  


9. sort_by
설명 : 시퀀스 내에 있는 해쉬값들도 소팅하게 한다.
         소트 방식은 sort와 동일하다.

예제 :
<#assign ls = [
  {"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>  

will print (with US locale at least):

Order by name:
- aardvark: 30
- Barbara: 53
- beetroot: 0.3
- whale: 2000
- zeppelin: -200

Order by weight:
- zeppelin: -200
- beetroot: 0.3
- aardvark: 30
- Barbara: 53
- whale: 2000  


만약에 좀더 많은 필드를 이용하여 깊은 레벨을 소팅하고자 한다면..
<#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>  

will print (with US locale at least):

Sorted by name.last: 
- Crooger, Fred: 35 years old
- Fox, Amanda: 25 years old
- Smith, Joe: 40 years old  


10. chunk
사용법 : 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>  

The output will be:

  a b c d 
  e f g h 
  i j 

  a b c d 
  e f g h 
  i j - - 


웹에서는 주로 2번째 파라미터를 지정할때  "\xA0" 을 주로 쓴다. 이것은 nbsp와 유사하다고 보면된다.

출처 : http://freemarker.sourceforge.net/docs/ref_builtins_sequence.html