freemarker標簽大全


FreeMarker的插值有如下兩種類型:1,通用插值${expr};2,數字格式化插值:#{expr}或#{expr;format} 
${book.name?if_exists }  //用於判斷如果存在,就輸出這個值 
${book.name?default(‘xxx’)}//默認值xxx 
${book.name!"xxx"}//默認值xxx 
${book.date?string('yyyy-MM-dd')} //日期格式 
${book?string.number}  20 //三種不同的數字格式 
${book?string.currency}--<#-- $20.00 --> 
${book?string.percent}—<#-- 20% --> 

<#assign foo=ture />   //聲明變量,插入布爾值進行顯示 
${foo?string("yes","no")} <#-- yes --> 

<等大小比較符號使用需要注意:(xml的原因),可以用於比較數字和日期 
使用lt、lte、gt和gte來替代<、<=、>和>= 也可以使用括號<#if (x>y)> 

內置函數: 調用區別於屬性的訪問,使用?代替. 
常見的一些內置函數 
對於字符串 
html-對字符串進行HTML編碼 
cap_first-使字符串第一個字母大寫 
lower_case-將字符串轉換成小寫 
trim-去掉字符串前后的空白字符 

對於Sequences(序列) 
size-獲得序列中元素的數目 

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

對於集合,可以使用數組的方式,使用下標索引進行訪問 

邏輯判斷: 
if................ 

<#if condition>... 
<#elseif condition2>... 
<#elseif condition3>...... 
<#else>... 
Boolean類型的空值判斷 
空值判斷可以寫成<#if book.name?? >   //注意${}為變量的渲染顯示,而<>為定義等操作符的定義 

switch............ 
<#switch value> 
<#case refValue1> 
    ... 
    <#break> 
<#case refValue2> 
    ... 
    <#break> 
... 
<#case refValueN> 
    ... 
    <#break> 
<#default> 
    ... 
</#switch> 

快速定義int區間的集合 
<#assign l=0..100/> //注意不需要[] 

3:循環讀取集合:  注意/的使用 
<#list student as stu> 
    ${stu}<br/> 
</#list> 
與jstl循環類似,也可以訪問循環的狀態 
item_index:當前變量的索引值 
item_has_next:是否存在下一個對象 其中item名稱為as后的變量名,如stu 

集合長度判斷 
<#if student?size != 0></#if>  判斷=的時候,注意只要一個=符號,而不是== 

宏/模板 
初步了解: 使用更像一個閉包closure,可以定義后,在腳本中任意地方引用,並原地起作用 
<#macro greet> 
<font size="+2">Hello Joe!</font> 
</#macro> 
使用的方式為: 
<@greet></@greet>  //同xml可以簡寫成<@greet/> 

宏的參數定義,類似js,在宏名后 帶參數進行傳遞定義 
<#macro greet person color> 
${person} 
</#macro> 

調用帶參數時,注意使用類似XML的屬性格式進行傳遞,不需要關心順序問題 
<@greet person="Fred" color="black"/> 

參數默認值定義,如果沒有,就必須要求傳遞完整的參數列表 
<#macro greet person color="black"> 
<font size="+2" color="${color}">Hello ${person}!</font> 
</#macro> 

使用xml的嵌套內容進行傳遞宏調用,關鍵標簽 <#nested> 
<#macro border> 
<table border=4 cellspacing=0 cellpadding=4><tr><td> 
    <#nested> 
</tr></td></table> 
</#macro> 

調用時: 
<@border>The bordered text</@border> 

<#nested> 標簽可以在宏中多次調用,也可以將多個宏組合進行嵌套 

for循環的精簡版: 
<#list 1..count as x> 
</#list> 

宏的循環變量,配合嵌套標簽進行參數傳遞, 
<#macro repeat count> 
<#list 1..count as x> 
    <#nested x, x/2, x==count>  //這里的三個參數,將會傳遞到嵌套內容中 
</#list> 
</#macro> 

<@repeat count=4 ; c, halfc, last> 
${c}. ${halfc}<#if last> Last!</#if> //這里的內容由macro中的<#nested>進行參數的傳遞,傳遞的數量任意,當注意需要宏接受這些 
</@repeat> 
上述還需要注意;的使用 

參數的數量是可變的,並不要求全部都有,但是效果不同 

在模板中定義變量 
在模板中定義的變量有三種類型: 
plain變量:可以在模板的任何地方訪問,包括使用include指令插入的模板,使用assign指令創建和替換。 
局部變量:在宏定義體中有效,使用local指令創建和替換。 
循環變量:只能存在於指令的嵌套內容,由指令(如list)自動創建;宏的參數是局部變量,而不是循環變量 

<#assign x = "plain"> //全局的plain變量 
內部循環變量將會隱藏同名的外部循環變量 

外部導入的使用,可以用於模塊化,並且提供公用性 
如:lib/my_lib.ftl文件 
<#macro copyright date> 
<p>Copyright (C) ${date} Julia Smith. All rights reserved. 
<br>Email: ${mail}</p> 
</#macro> 
<#assign mail = "jsmith@acme.com"> 

lib/my_inc.ftl文件 
<#import "/lib/my_test.ftl" as my> 
<#assign mail="fred@acme.com"> 
<@my.copyright date="1999-2002"/> 
${my.mail} 
${mail} 
輸出結果將不會出現沖突 

對於庫中的變量修改,使用in關鍵字 
<#assign mail="jsmith@other.com" in my> 

函數定義:區別於宏對象,帶返回值 
<#function name param1 param2><#return val></#function>函數,有返回參數 

stringA[M .. N] 取子字符串,類似substring(stringA, M, N) 

<#include "/copyright_footer.html"> 導入其他頁面元素 
<#include filename options> 
options包含兩個屬性 
encoding=”GBK” 編碼格式 
parse=true 是否作為ftl語法解析,默認是true,false就是以文本方式引入.注意在ftl文件里布爾值都是直接賦值的如parse=true,而不是 

parse=”true” 

hash與list的定義 
<#assign c= {"a":"orz","b":"czs"}> 
${c.a} 

List片段可以采用: products[10..19] or products[5..] 的格式進行定義,當只局限於數字 
<#assign c= [1,2,3,4,5,6,6,7]> 
    <#list c[1..3] as v> 
    ${v} 
    </#list> 

對變量的缺省處理 
product.color!"red" 

用compress directive或者transform來處理輸出。 
<#compress>...</#compress>:消除空白行。 
<@compress single_line=true>...</@compress>將輸出壓縮為一行。都需要包裹所需文檔 

freemarker可用"["代替"<".在模板的文件開頭加上[#ftl]. 

注釋部分 
<#-- 注釋部分 --> 

數字輸出的另外一種方式 
#{c.a;m0} 區別於${},這個例子是用於輸出數字的格式化,保留小數的位數,詳細如下 

數字格式化插值可采用#{expr;format}形式來格式化數字,其中format可以是: 
mX:小數部分最小X位 
MX:小數部分最大X位 

在定義字符串的時候,可以使用''或者"",對特殊字符,需要使用\進行轉義 

如果存在大量特殊字符,可以使用${r"..."}進行過濾 
${r"${foo}"} 
${r"C:\foo\bar"} 

Map對象的key和value都是表達式,但是key必須是字符串 
可以混合使用.和[""]訪問 
book.author["name"] //混合使用點語法和方括號語法 

為了處理缺失變量,FreeMarker提供了兩個運算符: 用於防止對象不存在而導致的異常 
!:指定缺失變量的默認值 
??:判斷某個變量是否存在,返回boolean值 

noparse指令指定FreeMarker不處理該指定里包含的內容,該指令的語法格式如下: 
<#noparse>...</#noparse> 

${firstName?html} 使用html對字符進行格式化處理,對於<等的過濾 

escape , noescape指令,對body內的內容實用統一的表達式 
看如下的代碼: 
<#escape x as x?html> 
First name:${firstName} 
Last name:${lastName} 
Maiden name:${maidenName} 
</#escape> 
上面的代碼等同於: 
First name:${firstName?html} 
Last name:${lastName?html} 
Maiden name:${maidenName?html} 

定義全局變量的方式 
<#assign name1=value1 name2=value2 / > // 可以同時定義多個變量,也可以使用循環來給變量賦值 
<#assign x> 
<#list ["星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"] as n> 
${n} 
</#list> 
</#assign> 
${x} 

setting指令,用於動態設置freeMarker的運行環境: 

該指令用於設置FreeMarker的運行環境,該指令的語法格式如下:<#setting name=value>,在這個格式中,name的取值范圍包含如下幾個: 
locale:該選項指定該模板所用的國家/語言選項 
number_format:指定格式化輸出數字的格式 
boolean_format:指定兩個布爾值的語法格式,默認值是true,false 
date_format,time_format,datetime_format:指定格式化輸出日期的格式 
time_zone:設置格式化輸出日期時所使用的時區 

<#return> 用於退出宏的運行 

?html 用於將字符串中可能包含的html字符,進行過濾. 

調用Java方法,需要使用實現TemplateMethodModel接口,但是好像會覆蓋掉屬性的訪問

 

替換字符串 replace
${s?replace(‘ba’, ‘XY’ )}
${s?replace(‘ba’, ‘XY’ , ‘規則參數’)}將s里的所有的ba替換成xy 規則參數包含: i r m s c f 具體含義如下:
· i: 大小寫不區分.
· f: 只替換第一個出現被替換字符串的字符串
· r: XY是正則表達式
· m: Multi-line mode for regular expressions. In multi-line mode the expressions ^ and $ match just after or just before, respectively, a line terminator or the end of the string. By default these expressions only match at the beginning and the end of the entire string.
· s: Enables dotall mode for regular expressions (same as Perl singe-line mode). In dotall mode, the expression . matches any character, including a line terminator. By default this expression does not match line terminators.
· c: Permits whitespace and comments in regular expressions.


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM