freemaker學習筆記--其它內置方法

一:數字型
<#assign answer=42/>
${answer}
${answer string}  <#-- the same as ${answer} -->
${answer string.number}
${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 if the value is a ...
 
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
 
is_enumerable
 sequence or collection
 
is_indexable
 sequence
 
is_directive
 macro or transform
 
is_node
 node
 

namespace:返回宏變量的命名空間,只能用於宏。

new:創建一個TemplateModel實現的變量。
<#assign word_wrapp = "com.acmee.freemarker.WordWrapperTransform" new()>
 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章