freemarker學習筆記

在模板中定義的變量有三種類型: html

引用java

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

  

局部變量隱藏(而不是覆蓋)同名的plain變量;循環變量隱藏同名的局部變量和plain變量 數組

<#assign x = "plain">    
1. ${x} <br>  
宏開始<br>  
<@test/>  
宏結束<br>  
6. ${x}  <br>    
    
<#list ["loop"] as x>    
    7. ${x}  <br>  
    <#assign x = "plain2">  <#-- replace the plain var -->    
    8*. ${x}  <br> <#-- it still hides the plain var. -->    
</#list>    
    
9*. ${x} <br>    
    
<#macro test>    
    2. ${x}  <br>    
    <#local x = "local">    
    3. ${x}  <br>  
  
    <#list ["loop"] as x>    
    4. ${x}  <br>    
    </#list>    
    5. ${x} <br>    
</#macro>     

  

 

模板中的變量會隱藏(而不是覆蓋)數據模型中同名變量,若是須要訪問數據模型中的同名變量,使用特殊變量global,下面的例子假設數據模型中的user的值是Big Joe: 
<#assign user = "Joe Hider"> 
${user}          <#-- prints: Joe Hider --> 
${.globals.user} <#-- prints: Big Joe --> 

jsp

一.輸出 

1:判斷list記錄大於2 ide

<#if (data?size>2)>  
    size 大於 2  
<#else>  
    size 小於 2  
</#if>   

2:插入布爾值: 函數

<#assign foo=false />  
${foo?string("yes","no")}    

3:默認值: oop

<#--<#assign name="liuwentao" />-->  
${name?default('xxx')} 

 

或者 this

 

<#--<#assign name="liuwentao" />-->  
${name!"xxx"}    

4:if_exists : url

<#assign name="liuwentao" />  
${name?if_exists }  

5:日期格式: spa

${current?string('yyyy-MM-dd HH:mm:ss E')}  

 6:數字格式:

<#setting number_format="#">  
${data.num}<br>  
${data.num?string.number}<br>  
${data.num?string.currency}<br>  
${data.num?string.percent}<br>  

 

 

 

 二.邏輯判斷 

1:if .. elseif .. else : 

<#assign name="劉文濤" />  
  
<#if !name??>  
    name這個變量不存在  
<#elseif name="liuwentao">  
    name的名字等於 liuwentao  
<#elseif name="劉文濤">  
    name的名字等於 劉文濤  
<#else>  
    name的名字等於 ${name}  
</#if>  

 空值判斷能夠寫成<#if name??>  

2:switch .. case .. default .. : 

<#assign name="劉文濤" />  
  
<#switch name>  
    <#case "liuwentao">  
        name的名字等於 liuwentao  
        <#break>  
    <#case "劉文濤">  
        name的名字等於 劉文濤  
        <#break>  
    <#default>  
        name的名字等於 ${name}  
</#switch>   

三.循環 

1:list : 

<#assign topics=data.list2 />  
  
<#list topics as topic>  
    ${topic_index}. ${topic.title2} <br>  
</#list>  

四. Macro(宏) 

1:基本用法 : 
宏是和某個變量關聯的模板片段,以便在模板中經過用戶定義指令使用該變量 

1.1:定義宏 

<#macro greet>  
    <font size="+2">Hello Joe!</font>  
</#macro>  

1.2:調用宏 

<@greet></@greet>  

 

或者 

 

<@greet/>  

調用宏時,與使用FreeMarker的其餘指令相似, 只是使用@替代FTL標記中的# 

2:帶參數的宏 
宏的參數是局部變量,只能在宏定義中有效 
2.1:帶一個參數的宏 

<#macro greet person>  
    <font size="+2">Hello ${person}!</font>  
</#macro>  
  
<@greet person="劉文濤"/>    

2.2:帶多個參數的宏 

<#macro greet person color>  
    <font size="+2" color="${color}">Hello ${person}!</font>  
</#macro>  
  
<@greet person="劉文濤" color="blue"/>  

2.3: 帶參數缺省值的宏 

<#macro greet person color="blue">  
    <font size="+2" color="${color}">Hello ${person}!</font>  
</#macro>  
<@greet person="劉文濤" color="yellow"/>  <br>  
<@greet person="劉文濤"/> 

 

 

 

3:在宏裏嵌套內容 

3.1:<#nested> 指令  

<#macro table>  
<table border=4 cellspacing=0 cellpadding=4>  
    <tr>  
        <td>第一次使用:<#nested></td>  
    </tr>  
    <tr>  
        <td>第二次使用:<#nested></td>  
    </tr>  
</table>  
</#macro>  
  
<@table>這些內容顯示在哪裏...</@table>  

  

 

 

3.2:嵌套內容能夠是有效的FTL 

<#macro greet person color="blue">  
    <font size="+2" color="${color}">Hello ${person}!</font>  
</#macro>  
  
<#macro table>  
<table border=4 cellspacing=0 cellpadding=4>  
    <tr>  
        <td>第一次使用:<#nested></td>  
    </tr>  
    <tr>  
        <td>第二次使用:<#nested></td>  
    </tr>  
</table>  
</#macro>  
  
<@table><@greet person="劉文濤"/></@table>  

  

 

4:循環變量 

4.1:基本用法 

<#macro repeat count>  
    <#list 1..count as x>  
        <#nested>  
    </#list>  
</#macro>  
  
<@repeat count=4>  
    freemarker...  
</@repeat> 

  

 

 

4.2:nestted指令也能夠有循環變量 

<#macro repeat count>  
     <#nested 2, 3, true>  
</#macro>  
  
<@repeat count=4 ; c, halfc, last>  
    ${c}. ${halfc} <#if last> Last!</#if> <br>  
</@repeat>  

 

 

這裏count是宏的參數,c, halfc,last則爲循環變量 

下面的寫法是不對的: 

<#macro repeat count>  
     <#nested 2, 3, true>  
</#macro>  
  
<@repeat count=4 ; c, halfc, last>  
    ${count}| ${c}. ${halfc} <#if last> Last!</#if> <br>  
</@repeat> 

由於:宏的參數是局部變量,而不是循環變量 


再看一個複雜一點例子: 

<#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> <br>  
</@repeat>  

循環變量和宏標記指定的不一樣不會有問題,若是調用時少指定了循環變量,那麼多餘的值不可見。調用時多指定了循環變量,多餘的循環變量不會被建立: 


五.命名空間 
一般狀況,只使用一個名字空間,稱爲主名字空間,但爲了建立可重用的宏、變換器或其它變量的集合(一般稱庫),必須使用多名字空間,其目的是防止同名衝突。 

1:建立庫(假設保存在lib/my_test.ftl中) 

<#macro copyright date>  
   <p>Copyright (C) ${date} Julia Smith. All rights reserved.  
   <br>Email: ${mail}</p>  
</#macro>   
<#assign mail="liuwentao@lib.com"> 

2:使用import指令導入庫到模板中 

Freemarker會爲導入的庫建立新的名字空間,並能夠經過import指令中指定的散列變量訪問庫中的變量 

<#import "/lib/my_test.ftl" as my>  
<#assign mail="wentao@outer.com">  
<@my.copyright date="1999-2002"/>  
${my.mail}  
${mail} 

輸出結果: 

<p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.  
<br>Email: liuwentao@lib.com</p>  
liuwentao@lib.com  
wentao@outer.com  

能夠看到例子中使用的兩個同名變量並無衝突,由於它們位於不一樣的名字空間。 


3:還可使用assign指令在導入的名字空間中建立或替代變量 

<#import "/lib/my_test.ftl" as my>  
${my.mail}  
<#assign mail="wentao@outer.com" in my>  
${my.mail}

輸出結果: 

liuwentao@lib.com  
wentao@outer.com    

4:數據模型中的變量任何地方均可見,也包括不一樣的名字空間 
下面是修改的庫: 

<#macro copyright date>  
   <p>Copyright (C) ${date} ${user}. All rights reserved.</p>  
</#macro>  
<#assign mail = "${user}@acme.com">  

假設數據模型中的user變量的值是liuwentao,則下面的代碼: 

<#import "/lib/my_test.ftl" as my>  
  
<@my.copyright date="1999-2002"/>  
${my.mail}  

  輸出結果: 

<p>Copyright (C) 1999-2002 liuwentao. All rights reserved.</p>liuwentao@acme.com  

  

我遇到的問題 

1: html 轉義 

<tr>  
    <td>姓名:</td>  
    <td><strong>${item.realName!?html}</strong></td>  
</tr>  

  

 

指定變量 

<#assign categoryId =data.categoryId/>    

調用靜態方法 

<#assign category=statics["com.koolearn.eclass.product.ProductFactory"].getProductCategory(categoryId) />  

  

 

<#assign util=statics["com.koolearn.security.base.utils.KoolearnStringUtils"] />  
${util.subStrUnicode(item.content!?html,data.width!20,true)}    

1.經常使用的函數 

fm的函數調用語法是變量名後面跟一個問號,再寫上方法名,如:${test?upper_case} 
若是要調用2個以上的內置方法,則連續寫,如:${test?upper_case?html} 

引用
 
html : 將特殊html標記進行轉換,如<轉換成< 
cap_first : 首字母大寫 
lower_case : 轉換成小寫 
upper_case :轉換成大寫 
trim : 剪掉先後空白 
size :獲得序列、數組的元素個數 
int :得到小數的整數部分 
substring (from,  toExclusive):取子串 
contains :和java同 
ends_with :  以…結尾 嗎 
starts_with :  以…開頭嗎 
last_index_of :  最後的索引所在位置 
length :  字符串長度 
matches :是否 匹配 一個正則

  

replace :  替換 

${"this is a car acarus"?replace("car", "bulldozer")}----------->  
this is a bulldozer abulldozerus 

  

2.刪除連續多個變量之間的換行符 

設想這樣一個應用場景,你想生成一個java方法的代碼,方法的參數是多個的,你必定不但願它是一行一個參數吧,那你就須要使用<#t>標籤了,它的意思是去掉當前行首尾的空白,注意,換行也當成空白,好比下面的例子:

1 <#t>     
2<#t>     
3<#lt>     
4     
5<#rt>     
6    

  

執行後的結果是: 

1 23   
4   
5 6  

<#t>在行裏的位置無所謂,相似還有<#lt><#rt>,意思是去掉左側和右側的空白。 

3.如何判斷變量是否存在或對象的屬性是否爲null 
  用??進行判斷,以下: 

<#if user??><h1>Welcome ${user}!</h1></#if>   

 存在或有值則爲true 

4.如何輸出${xxx} 這樣的字符串 

  jsp el的語法和fm很像,都支持${}這樣來輸出變量內容,固然jsp自己其實也是一種模板技術,兩者本質是同樣的,你若是想生成jsp el的代碼就會遇到這個問題,不能直接寫,不然fm會把它當成fm的東西來解析成它變量了,這時候須要使用<#noparse>標籤。 

<#noparse>${ccc}</#noparse>    

這樣fm就不會把它當成變量來解析而是直接輸出 ${ccc} 了 

chunk 用法 
<table width="950" border="0" cellspacing="2" cellpadding="0" align="center" class="mb12">  
<#list bBookList?chunk(5) as row>  
    <tr>  
        <#list row as item>  
            <td align="center">  
                <#if item.name??>  
                     <#assign name=util.subString(item.name!,item.shortName!,data.width)/>  
                    <a title='${item.name!}' href="${item.urlDetail!}" target="_blank">  
                        <img src="${item.photoS!}" alt="${item.name!}" width="145" height="110" border="0" class="img_border2">  
                    </a>  
                    <table border="0" cellpadding="0" cellspacing="0">  
                        <tr>  
                            <td height="36" align="center">  
                                <a title='${item.name!}' href="${item.urlDetail!}" target="_blank">  
                                    ${name}  
                                </a>  
                            </td>  
                        </tr>  
                    </table>  
                </#if>  
            </td>  
        </#list>  
    </tr>  
</#list>  
</table>  

  

 

原文地址:http://wentao365.iteye.com/blog/612293

相關文章
相關標籤/搜索