Built-ins for sequences

first

The first subvariable of the sequence. Template processing will die with error if the sequence is empty.

last

The last subvariable of the sequence. Template processing will die with error if the sequence is empty.

reverse

The sequence with reversed order.

size

The number of subvariables in sequence (as a numerical value). The highest possible index in sequence s is s?size - 1 (since the index of the first subvariable is 0) assuming that the sequence has at least one subvariable.

sort

Returns the sequence sorted in ascending order. This will work only if all subvariables are strings, or if all subvariables are numbers. If the subvariables are strings, it uses locale (language) specific lexical sorting. For example:

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

sort_by

Returns the sequence of hashes sorted by the given hash subvariable in ascending order. The rules are the same as with the sort built-in, except that the subvariables of the sequence must be hashes, and you have to give the name of a hash subvariable that will decide the order. For example:

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

Page generated: 2004-06-15 22:17:59 GMT FreeMarker Manual -- For FreeMarker 2.3