天天看点

freemaker学习笔记--内置方法

一:数字型

<#assign answer=42/>

${answer}

${answer?string}  <#-- the same as ${answer} -->

${answer?string.number}(文章来源 www.iocblog.net)

${answer?string.currency}

${answer?string.percent}

<#setting number_format="0.###E0"/>

${12345?string("0.####E0")}  

${answer?string("number")} 等同于:${answer?string.number}.

二:日期型:

预定义格式包括:short ,middle和long。

${openingTime?string.short}

${openingTime?string.medium}

${openingTime?string.long}

组合使用:${lastUpdated?string.short_long}

${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}

三:逻辑型

foo?string

foo?string("yes", "no")

四:序列的内置方法:

first

last

seq_contains

${x?seq_contains("blue")?string("yes", "no")}

seq_index_of

seq_last_index_of

reverse

size

sort

sort_by

多层次的hash排序

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

chunk:将一个序列分解成几个序列。

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

chunk经常用于分栏或者表格输出的格式。

五:hash内置方法:

keys:

values:

六:很少使用或者专家级的内置方法:

byte,double,float,int,long,short。

eval

has_content:变量存在,不是指null也不是空串,一般情况下可以使用expr!?size > 0 or expr!?length > 0 代替expr?has_content。

interpret将一个字符串解析为ftl模版,产生一个TemplateTransformModel对象。

<#assign x=["a", "b", "c"]>

<#assign templateSource = "<#list x as y>${y}</#list>">

<#assign inlineTemplate = templateSource?interpret>

<@inlineTemplate>def/@inlineTemplate

is_...检查变量类型:

Built-in Returns true
is_string string
is_number number
is_boolean boolean
is_date date (all types: date-only, time-only and date-time)
is_method method
is_transform transform
is_macro macro
is_hash hash
is_hash_ex extended hash (i.e. supports ?keys and ?values)
is_sequence sequence
is_collection collection(文章来源 www.iocblog.net)
is_enumerable sequence or collection
is_indexable sequence
is_directive macro or transform