freemarker 整理手冊

2021-06-01 23:23:26 字數 3147 閱讀 9749

注釋:包含在<#--和-->

<#local y = "test"> 定義區域性變數

<#import "/lib/my_test.ftl" as my> 匯入模板檔案 指定名字空間 my

<#macro greet>     使用者定義指令

hello joe!  

ø         作為使用者定義指令使用巨集變數時,使用@替代ftl標記中的#

<@greet>

ø         如果沒有體內容,也可以使用:

<@greet/>

<#macro greet person>

hello $!

ø         可以這樣使用這個巨集變數:

<@greet person="fred"/> and <@greet person="batman"/>

輸出結果是:

hello fred!

and   hello batman!

使用者定義指令可以有巢狀內容,使用<#nested>指令執行指令開始和結束標記之間的模板片斷

ø         例子:

<#macro border>

<#nested>  

if指令

<#if animals.python.price < animals.elephant.price>

pythons are cheaper than elephants today.

<#else>

pythons are not cheaper than elephants today. 

list指令

<#list animals as being>

$$ euros

<#list ["winter", "spring", "summer", "autumn"] as x>

$<#list ["joe", "fred"] + ["julia", "kate"] as user>

- $輸出結果是:

- joe

- fred

- julia

- kate

include指令

有一類特殊的字串稱為raw字串,被認為是純文字,其中的\和"}

$ 輸出的結果是:

$c:\foo\bar 

<#assign ages = + >

- joe is $

- fred is $

- julia is $ 

輸出結果是:

- joe is 30

- fred is 25

- julia is 18 

由於freemarker會將》解釋成ftl標記的結束字元,所以對於》和》=可以使用括號來避免這種情況,例如

<#if (x > y)>

內建函式

ø         內建函式的用法類似訪問雜湊的子變數,只是使用「?」替代「.」,下面列出常用的一些函式

ø         字串使用的:

n         html:對字串進行html編碼

n         cap_first:使字串第乙個字母大寫

n         lower_case:將字串轉換成小寫

n         upper_case:將字串轉換成大寫

n         trim:去掉字串前後的空白字元

ø         序列使用的:

n         size:獲得序列中元素的數目

ø         數字使用的:

n         int:取得數字的整數部分(如-1.9?int的結果是-1)

ø         例子(假設test儲存字串"tom & jerry"):$$

輸出結果是:

tom & jerry

tom & jerry 

string格式化單個interpolation,下面是乙個例子:

<#setting number_format="currency"/>

<#assign answer=42/>

$$  <#-- the same as $ -->$$

$輸出結果是:

$42.00

$42.00

42$42.00

4,200%

ø         插入日期值:根據預設格式(由#setting指令設定)將表示式結果轉換成文字輸出;可以使用內建函式string格式化單個interpolation,下面是乙個使用格式模式的例子:$$

$ 輸出的結果類似下面的格式:

2003-04-08 21:24:44 pacific daylight time

tue, apr 8, '03

tuesday, april 08, 2003, 09:24:44 pm (pdt)

ø         插入布林值:根據預設格式(由#setting指令設定)將表示式結果轉換成文字輸出;可以使用內建函式string格式化單個interpolation,下面是乙個例子:

<#assign foo=true/>

$輸出結果是:

yes

數字interpolation的#形式可以用來格式化數字,format可以是:

ø         mx:小數部分最小x位

ø         mx:小數部分最大x位

ø         例子:

<#-- if the language is us english the output is: -->

<#assign x=2.582/>

<#assign y=4/>

#   <#-- 2.58 -->

#   <#-- 4    -->

#   <#-- 2.6 -->

#   <#-- 4.0 -->

# <#-- 2.58 -->

# <#-- 4.0  -->

使用import指令導入庫到模板中,freemarker會為匯入的庫建立新的名字空間,並可以通過import指令中指定的雜湊變數訪問庫中的變數:

<#import "/lib/my_test.ftl" as my>

<#assign mail="[email protected]">$$ 

輸出結果:

email: [email protected]>

[email protected]

[email protected]

FreeMarker幫助手冊

freemarker學習1 ftl 2008 07 31 13 47 中存放所有freemaker的內容,之外的內容全部原樣輸出。是函式呼叫 兩個定界符內的內容中,第乙個符號表示指令或者函式名,其後的跟隨引數。freemaker提供的控制包括如下 if condition elseif condit...

FineUI初學手冊 部分JS整理

有人老找js,我吧fineui自己寫的js沾過來方便大家看看,在實現前端的時候更靈活 js例項 注釋控制項 f.ready f.ready function 就是ready 很多方法都在這裡寫,寫在頁面後 無f.alert f.alert 提示 fineui 的提示框 無f.confirm conf...

軟負載方案整理配置手冊

因為需要,嘗試了預研下面常用幾個軟負載,大概整理了如下 1.lvs效能最強 1.lvs linux virtual server vs 請求對於realserver rs 都是透明的 不修改src 使用核心 相比應用實現效能要高很多。分為3種模式 l dr 效能最好 在vs 和rs 使用相同的vip...