`

Freemarker页面语法

阅读更多

A 概念

最常用的 3 个概念

sequence 序列,对应java 里的list 、数组等非键值对的集合

hash 键值对的集合

namespace 对一个ftl 文件的引用, 利用这个名字可以访问到该ftl 文件的资源

B 指令

if, else, elseif

语法

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23if%20condition%3E%0A%20%20...%0A%3C%23elseif%20condition2%3E%0A%20%20...%0A%3C%23elseif%20condition3%3E%0A%20%20...%0A...%0A%3C%23else%3E%0A%20%20...%0A%3C%2F%23if%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <# if condition>
  2. ...
  3. <#elseif condition2>
  4. ...
  5. <#elseif condition3>
  6. ...
  7. ...
  8. <#else >
  9. ...
  10. </#if >
<#if condition>
  ...
<#elseif condition2>
  ...
<#elseif condition3>
  ...
...
<#else>
  ...
</#if>

用例

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23if%20x%20%3D%201%3E%0A%20%20x%20is%201%0A%3C%2F%23if%3E%0A%3C%23if%20x%20%3D%201%3E%0A%20%20x%20is%201%0A%3C%23else%3E%0A%20%20x%20is%20not%201%0A%3C%2F%23if%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#if x = 1 >
  2. x is 1
  3. </#if>
  4. <#if x = 1 >
  5. x is 1
  6. <#else>
  7. x is not 1
  8. </#if>
<#if x = 1>
  x is 1
</#if>
<#if x = 1>
  x is 1
<#else>
  x is not 1
</#if>

switch, case, default, break

语法

 

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23switch%20value%3E%0A%20%20%3C%23case%20refValue1%3E%0A%20%20%20%20%20%20%20%20%20...%0A%20%20%20%20%20%20%20%20%20%3C%23break%3E%0A%20%20%3C%23case%20refValue2%3E%0A%20%20%20%20%20%20%20%20%20...%0A%20%20%20%20%20%20%20%20%20%3C%23break%3E%0A%20%20...%0A%20%20%3C%23case%20refValueN%3E%0A%20%20%20%20%20%20%20%20%20...%0A%20%20%20%20%20%20%20%20%20%3C%23break%3E%0A%20%20%3C%23default%3E%0A%20%20%20%20%20%20%20%20%20...%0A%3C%2F%23switch%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#switch value>
  2. <#case refValue1>
  3. ...
  4. <#break>
  5. <#case refValue2>
  6. ...
  7. <#break>
  8. ...
  9. <#case refValueN>
  10. ...
  11. <#break>
  12. <#default>
  13. ...
  14. </#switch>
<#switch value>
  <#case refValue1>
         ...
         <#break>
  <#case refValue2>
         ...
         <#break>
  ...
  <#case refValueN>
         ...
         <#break>
  <#default>
         ...
</#switch>

用例

字符串

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23switch%20being.size%3E%0A%20%20%3C%23case%20%22small%22%3E%0A%20%20%20%20%20%20%20%20%20%20This%20will%20be%20processed%20if%20it%20is%20small%0A%20%20%20%20%20%20%20%20%20%20%3C%23break%3E%0A%20%20%3C%23case%20%22medium%22%3E%0A%20%20%20%20%20%20%20%20%20%20This%20will%20be%20processed%20if%20it%20is%20medium%0A%20%20%20%20%20%20%20%20%20%20%3C%23break%3E%0A%20%20%3C%23case%20%22large%22%3E%0A%20%20%20%20%20%20%20%20%20%20This%20will%20be%20processed%20if%20it%20is%20large%0A%20%20%20%20%20%20%20%20%20%20%3C%23break%3E%0A%20%20%3C%23default%3E%0A%20%20%20%20%20%20%20%20%20%20This%20will%20be%20processed%20if%20it%20is%20neither%0A%3C%2F%23switch%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#switch being.size>
  2. <#case "small" >
  3. This will be processed if it is small
  4. <#break>
  5. <#case "medium" >
  6. This will be processed if it is medium
  7. <#break>
  8. <#case "large" >
  9. This will be processed if it is large
  10. <#break>
  11. <#default>
  12. This will be processed if it is neither
  13. </#switch>
<#switch being.size>
  <#case "small">
          This will be processed if it is small
          <#break>
  <#case "medium">
          This will be processed if it is medium
          <#break>
  <#case "large">
          This will be processed if it is large
          <#break>
  <#default>
          This will be processed if it is neither
</#switch>

数字

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23switch%20x%3E%0A%20%20%3C%23case%20x%20%3D%201%3E%0A%20%20%20%20%20%20%20%20%201%0A%20%20%3C%23case%20x%20%3D%202%3E%0A%20%20%20%20%20%20%20%20%202%0A%20%20%3C%23default%3E%0A%20%20%20%20%20%20%20%20%20d%0A%3C%2F%23switch%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#switch x>
  2. <#case x = 1 >
  3. 1
  4. <#case x = 2 >
  5. 2
  6. <#default>
  7. d
  8. </#switch>
<#switch x>
  <#case x = 1>
         1
  <#case x = 2>
         2
  <#default>
         d
</#switch>

如果x=1 输出 1 2, x=2 输出 2, x=3 输出d

list, break

语法

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23list%20sequence%20as%20item%3E%0A...%0A%3C%23if%20item%20%3D%20%22spring%22%3E%3C%23break%3E%3C%2F%23if%3E%0A...%0A%3C%2F%23list%3E%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#list sequence as item>
  2. ...
  3. <#if item = "spring" ><#break></#if>
  4. ...
  5. </#list>
<#list sequence as item>
...
<#if item = "spring"><#break></#if>
...
</#list>

关键字

item_index:是list当前值的下标

item_has_next:判断list是否还有值

用例

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23assign%20seq%20%3D%20%5B%22winter%22%2C%20%22spring%22%2C%20%22summer%22%2C%20%22autumn%22%5D%3E%0A%3C%23list%20seq%20as%20x%3E%0A%20%20%24%7Bx_index%20%2B%201%7D.%20%24%7Bx%7D%3C%23if%20x_has_next%3E%2C%3C%2F%23if%3E%0A%3C%2F%23list%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#assign seq = [ "winter" , "spring" , "summer" , "autumn" ]>
  2. <#list seq as x>
  3. ${x_index + 1 }. ${x}<#if x_has_next>,</#if>
  4. </#list>
<#assign seq = ["winter", "spring", "summer", "autumn"]>
<#list seq as x>
  ${x_index + 1}. ${x}<#if x_has_next>,</#if>
</#list>

 

输出:

1.winter,

2.spring,

3.summer,

4.autumn

include

语法

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23include%20filename%3E%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#include filename>
<#include filename>

或则

Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23include%20filename%20options%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#include filename options>
<#include filename options>

options包含两个属性

encoding="GBK" 编码格式

parse=true 是否作为ftl语法解析,默认是true,false就是以文本方式引入.注意在ftl文件里布尔值都是直接赋值
的如parse=true,而不是parse="true"

用例

/common/copyright.ftl 包含内容

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=Copyright%202001-2002%20%24%7Bme%7D%0A%0AAll%20rights%20reserved.%20%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. Copyright 2001 - 2002 ${me}
  2. All rights reserved.
Copyright 2001-2002 ${me}

All rights reserved.  

模板文件


Java代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23assign%20me%20%3D%20%22Juila%20Smith%22%3E%0A%0ASome%20test%0A%0AYeah%0A___________________________________________________________________________%0A%0A%3Cspan%3E%3Cstrong%3E%3Cspan%3E%3C%23include%20%22%2Fcommon%2Fcopyright.ftl%22%20encoding%3D%22GBK%22%3E%3C%2Fspan%3E%0A%0A%0A%0A%0A%3C%2Fstrong%3E%0A%0A%0A%3C%2Fspan%3E%0A%0A%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#assign me = "Juila Smith" >
  2. Some test
  3. Yeah
  4. ___________________________________________________________________________
  5. <span><strong><span><#include "/common/copyright.ftl" encoding= "GBK" ></span>
  6. </strong>
  7. </span>
<#assign me = "Juila Smith">

Some test

Yeah
___________________________________________________________________________

<#include "/common/copyright.ftl" encoding="GBK">
















 


输出结果:

Some test

Yeah.

Copyright 2001-2002 Juila Smith

All rights reserved.

 

Import

语法

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23import%20path%20as%20hash%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#import path as hash>
<#import path as hash>

类似于java里的import,它导入文件,然后就可以在当前文件里使用被导入文件里的宏组件

用例

假设mylib.ftl 里定义了宏copyright 那么我们在其他模板页面里可以这样使用

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23import%20%22%2Flibs%2Fmylib.ftl%22%20as%20my%3E%0A%0A%3C%40my.copyright%20date%3D%221999-2002%22%2F%3E%0A%0A%3C%23--%20%22my%22%E5%9C%A8freemarker%E9%87%8C%E8%A2%AB%E7%A7%B0%E4%BD%9Cnamespace%20--%3E%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#import "/libs/mylib.ftl" as my>
  2. <@my.copyright date="1999-2002" />
  3. <#-- "my" 在freemarker里被称作namespace -->
<#import "/libs/mylib.ftl" as my>

<@my.copyright date="1999-2002"/>

<#-- "my"在freemarker里被称作namespace -->

compress

语法

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23compress%3E%0A%20%20...%0A%3C%2F%23compress%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#compress>
  2. ...
  3. </#compress>
<#compress>
  ...
</#compress>

用来压缩空白空间和空白的行

escape, noescape

语法

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23escape%20identifier%20as%20expression%3E%0A%20%20...%0A%20%20%3C%23noescape%3E...%3C%2F%23noescape%3E%0A%20%20...%0A%3C%2F%23escape%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#escape identifier as expression>
  2. ...
  3. <#noescape>...</#noescape>
  4. ...
  5. </#escape>
<#escape identifier as expression>
  ...
  <#noescape>...</#noescape>
  ...
</#escape>

用例

主要使用在相似的字符串变量输出,比如某一个模块的所有字符串输出都必须是html安全的,这个时候就可以使用

该表达式

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23escape%20x%20as%20x%3Fhtml%3E%0A%20%20First%20name%3A%20%24%7BfirstName%7D%0A%20%20%3C%23noescape%3ELast%20name%3A%20%24%7BlastName%7D%3C%2F%23noescape%3E%0A%20%20Maiden%20name%3A%20%24%7BmaidenName%7D%0A%3C%2F%23escape%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#escape x as x?html>
  2. First name: ${firstName}
  3. <#noescape>Last name: ${lastName}</#noescape>
  4. Maiden name: ${maidenName}
  5. </#escape>
<#escape x as x?html>
  First name: ${firstName}
  <#noescape>Last name: ${lastName}</#noescape>
  Maiden name: ${maidenName}
</#escape>

相同表达式

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%20%20First%20name%3A%20%24%7BfirstName%3Fhtml%7D%0A%20%20Last%20name%3A%20%24%7BlastName%20%7D%0A%20%20Maiden%20name%3A%20%24%7BmaidenName%3Fhtml%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. First name: ${firstName?html}
  2. Last name: ${lastName }
  3. Maiden name: ${maidenName?html}
  First name: ${firstName?html}
  Last name: ${lastName }
  Maiden name: ${maidenName?html}

assign

语法

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23assign%20name%3Dvalue%3E%0A%0A%3C%23--%20%E6%88%96%E5%88%99%20--%3E%0A%0A%3C%23assign%20name1%3Dvalue1%20name2%3Dvalue2%20...%20nameN%3DvalueN%3E%0A%0A%3C%23--%20%E6%88%96%E5%88%99%20--%3E%0A%0A%3C%23assign%20same%20as%20above...%20in%20namespacehash%3E%0A%0A%3C%23--%20%E6%88%96%E5%88%99%20--%3E%0A%0A%3C%23assign%20name%3E%0A%20%20capture%20this%0A%3C%2F%23assign%3E%0A%0A%3C%23--%20%E6%88%96%E5%88%99%20--%3E%0A%0A%3C%23assign%20name%20in%20namespacehash%3E%0A%20%20capture%20this%0A%3C%2F%23assign%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#assign name=value>
  2. <#-- 或则 -->
  3. <#assign name1=value1 name2=value2 ... nameN=valueN>
  4. <#-- 或则 -->
  5. <#assign same as above... in namespacehash>
  6. <#-- 或则 -->
  7. <#assign name>
  8. capture this
  9. </#assign>
  10. <#-- 或则 -->
  11. <#assign name in namespacehash>
  12. capture this
  13. </#assign>
<#assign name=value>

<#-- 或则 -->

<#assign name1=value1 name2=value2 ... nameN=valueN>

<#-- 或则 -->

<#assign same as above... in namespacehash>

<#-- 或则 -->

<#assign name>
  capture this
</#assign>

<#-- 或则 -->

<#assign name in namespacehash>
  capture this
</#assign>

用例

生成变量,并且给变量赋值

给seasons赋予序列值

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23assign%20seasons%20%3D%20%5B%22winter%22%2C%20%22spring%22%2C%20%22summer%22%2C%20%22autumn%22%5D%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#assign seasons = [ "winter" , "spring" , "summer" , "autumn" ]>
<#assign seasons = ["winter", "spring", "summer", "autumn"]>

给变量test加1

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23assign%20test%20%3D%20test%20%2B%201%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#assign test = test + 1 >
<#assign test = test + 1>

给my namespage 赋予一个变量bgColor,下面可以通过my.bgColor来访问这个变量

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23import%20%22%2Fmylib.ftl%22%20as%20my%3E%0A%0A%3C%23assign%20bgColor%3D%22red%22%20in%20my%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#import "/mylib.ftl" as my>
  2. <#assign bgColor="red" in my>
<#import "/mylib.ftl" as my>

<#assign bgColor="red" in my>

将一段输出的文本作为变量保存在x里

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23assign%20x%3E%0A%20%20%3C%23list%201..3%20as%20n%3E%0A%20%20%20%20%20%20%20%20%20%24%7Bn%7D%20%3C%40myMacro%20%2F%3E%0A%20%20%3C%2F%23list%3E%0A%3C%2F%23assign%3E%0A%0ANumber%20of%20words%3A%20%24%7Bx%3Fword_list%3Fsize%7D%0A%0A%24%7Bx%7D%0A%0A%3C%23assign%20x%3EHello%20%24%7Buser%7D!%3C%2F%23assign%3E%20%20%20%20%20%20%20%20%20%20error%0A%0A%3C%23assign%20x%3D%22%20Hello%20%24%7Buser%7D!%22%3E%20%20%20%20%20%20%20%20%20true" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#assign x>
  2. <#list 1 .. 3 as n>
  3. ${n} <@myMacro />
  4. </#list>
  5. </#assign>
  6. Number of words: ${x?word_list?size}
  7. ${x}
  8. <#assign x>Hello ${user}!</#assign> error
  9. <#assign x=" Hello ${user}!" > true
<#assign x>
  <#list 1..3 as n>
         ${n} <@myMacro />
  </#list>
</#assign>

Number of words: ${x?word_list?size}

${x}

<#assign x>Hello ${user}!</#assign>          error

<#assign x=" Hello ${user}!">         true

同时也支持中文赋值,如:

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23assign%20%E8%AF%AD%E6%B3%95%3E%0A%20%20java%0A%3C%2F%23assign%3E%0A%0A%24%7B%E8%AF%AD%E6%B3%95%7D" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#assign 语法>
  2. java
  3. </#assign>
  4. ${语法}
<#assign 语法>
  java
</#assign>

${语法}

打印输出:

java


global

语法

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23global%20name%3Dvalue%3E%0A%0A%3C%23--%E6%88%96%E5%88%99--%3E%0A%0A%3C%23global%20name1%3Dvalue1%20name2%3Dvalue2%20...%20nameN%3DvalueN%3E%0A%0A%3C%23--%E6%88%96%E5%88%99--%3E%0A%0A%3C%23global%20name%3E%0A%20%20capture%20this%0A%3C%2F%23global%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#global name=value>
  2. <#--或则-->
  3. <#global name1=value1 name2=value2 ... nameN=valueN>
  4. <#--或则-->
  5. <#global name>
  6. capture this
  7. </#global>
<#global name=value>

<#--或则-->

<#global name1=value1 name2=value2 ... nameN=valueN>

<#--或则-->

<#global name>
  capture this
</#global>

全局赋值语法,利用这个语法给变量赋值,那么这个变量在所有的namespace [A1] 中是可见的, 如果这个变量被当前的assign 语法覆盖 如<#global x=2> <#assign x=1> 在当前页面里x=2 将被隐藏,或者通过${.global.x} 来访问

setting

语法

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23setting%20name%3Dvalue%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#setting name=value>
<#setting name=value>

用来设置整个系统的一个环境

locale

number_format

boolean_format

date_format , time_format , datetime_format

time_zone

classic_compatible

用例

假如当前是匈牙利的设置,然后修改成美国

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%24%7B1.2%7D%0A%0A%3C%23setting%20locale%3D%22en_US%22%3E%0A%0A%24%7B1.2%7D%20%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. ${ 1.2 }
  2. <#setting locale="en_US" >
  3. ${1.2 }
${1.2}

<#setting locale="en_US">

${1.2}  

输出

1,2

1.2

因为匈牙利是采用", "作为十进制的分隔符,美国是用". "

macro, nested, return

语法

Freemarker代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23macro%20name%20param1%20param2%20...%20paramN%3E%0A%20%20...%0A%20%20%3C%23nested%20loopvar1%2C%20loopvar2%2C%20...%2C%20loopvarN%3E%0A%20%20...%0A%20%20%3C%23return%3E%0A%20%20...%0A%3C%2F%23macro%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#macro name param1 param2 ... paramN>
  2. ...
  3. <#nested loopvar1, loopvar2, ..., loopvarN>
  4. ...
  5. <#return>
  6. ...
  7. </#macro>
<#macro name param1 param2 ... paramN>
  ...
  <#nested loopvar1, loopvar2, ..., loopvarN>
  ...
  <#return>
  ...
</#macro>

用例

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23macro%20test%20foo%20bar%3D%22Bar%22%5BA2%5D%20%20baaz%3D-1%3E%0A%20%20Test%20text%2C%20and%20the%20params%3A%20%24%7Bfoo%7D%2C%20%24%7Bbar%7D%2C%20%24%7Bbaaz%7D%0A%3C%2F%23macro%3E%0A%0A%3C%40test%20foo%3D%22a%22%20bar%3D%22b%22%20baaz%3D5*5-2%2F%3E%0A%0A%3C%40test%20foo%3D%22a%22%20bar%3D%22b%22%2F%3E%0A%0A%3C%40test%20foo%3D%22a%22%20baaz%3D5*5-2%2F%3E%0A%0A%3C%40test%20foo%3D%22a%22%2F%3E%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#macro test foo bar= "Bar" [A2] baaz=- 1 >
  2. Test text, and the params: ${foo}, ${bar}, ${baaz}
  3. </#macro>
  4. <@test foo="a" bar= "b" baaz= 5 * 5 - 2 />
  5. <@test foo="a" bar= "b" />
  6. <@test foo="a" baaz= 5 * 5 - 2 />
  7. <@test foo="a" />
<#macro test foo bar="Bar"[A2]  baaz=-1>
  Test text, and the params: ${foo}, ${bar}, ${baaz}
</#macro>

<@test foo="a" bar="b" baaz=5*5-2/>

<@test foo="a" bar="b"/>

<@test foo="a" baaz=5*5-2/>

<@test foo="a"/> 

输出

Test text, and the params: a, b, 23
Test text, and the params: a, b, -1
Test text, and the params: a, Bar, 23

Test text, and the params: a, Bar, -1

 

定义循环输出的宏

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23macro%20list%20title%20items%3E%0A%24%7Btitle%3Fcap_first%7D%3A%0A%20%20%20%20%20%20%20%20%3C%23list%20items%20as%20x%3E%0A%20%20%20%20%20%20%20%20%20%20%20*%24%7Bx%3Fcap_first%7D%0A%0A%20%20%20%20%20%20%20%20%20%3C%2F%23list%3E%0A%3C%2F%23macro%3E%0A%0A%3C%40list%20items%3D%5B%22mouse%22%2C%20%22elephant%22%2C%20%22python%22%5D%20title%3D%22Animals%22%2F%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#macro list title items>
  2. ${title?cap_first}:
  3. <#list items as x>
  4. *${x?cap_first}
  5. </#list>
  6. </#macro>
  7. <@list items=["mouse" , "elephant" , "python" ] title= "Animals" />
<#macro list title items>
${title?cap_first}:
        <#list items as x>
           *${x?cap_first}

         </#list>
</#macro>

<@list items=["mouse", "elephant", "python"] title="Animals"/>

输出结果:
Animals:
*Mouse
*Elephant
*Python

 

包含body 的宏

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23macro%20repeat%20count%3E%0A%20%20%3C%23list%201..count%20as%20x%3E%0A%20%20%20%20%20%20%20%20%20%3C%23nested%20x%2C%20x%2F2%2C%20x%3D%3Dcount%3E%0A%20%20%3C%2F%23list%3E%0A%3C%2F%23macro%3E%0A%0A%3C%40repeat%20count%3D4%20%3B%20c%20halfc%20last%3E%0A%20%20%24%7Bc%7D.%20%24%7Bhalfc%7D%3C%23if%20last%3E%20Last!%3C%2F%23if%3E%0A%3C%2F%40repeat%3E%20" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#macro repeat count>
  2. <#list 1 ..count as x>
  3. <#nested x, x/2 , x==count>
  4. </#list>
  5. </#macro>
  6. <@repeat count=4 ; c halfc last>
  7. ${c}. ${halfc}<#if last> Last!</#if>
  8. </@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>
</@repeat> 

输出

1. 0.5
2. 1
3. 1.5

4. 2 Last!

t, lt, rt

语法

Freemarkder代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23t%3E%20%E5%8E%BB%E6%8E%89%E5%B7%A6%E5%8F%B3%E7%A9%BA%E7%99%BD%E5%92%8C%E5%9B%9E%E8%BD%A6%E6%8D%A2%E8%A1%8C%0A%0A%3C%23lt%3E%E5%8E%BB%E6%8E%89%E5%B7%A6%E8%BE%B9%E7%A9%BA%E7%99%BD%E5%92%8C%E5%9B%9E%E8%BD%A6%E6%8D%A2%E8%A1%8C%0A%0A%3C%23rt%3E%E5%8E%BB%E6%8E%89%E5%8F%B3%E8%BE%B9%E7%A9%BA%E7%99%BD%E5%92%8C%E5%9B%9E%E8%BD%A6%E6%8D%A2%E8%A1%8C%0A%0A%3C%23nt%3E%E5%8F%96%E6%B6%88%E4%B8%8A%E9%9D%A2%E7%9A%84%E6%95%88%E6%9E%9C" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#t> 去掉左右空白和回车换行
  2. <#lt>去掉左边空白和回车换行
  3. <#rt>去掉右边空白和回车换行
  4. <#nt>取消上面的效果
<#t> 去掉左右空白和回车换行

<#lt>去掉左边空白和回车换行

<#rt>去掉右边空白和回车换行

<#nt>取消上面的效果

C 一些常用方法或注意事项

表达式转换类

${expression} 计算expression 并输出

#{ expression } 数字计算#{ expression ;format} 安格式输出数字formatMm

M 表示小数点后最多的位数,m 表示小数点后最少的位数如#{121.2322;m2M2} 输出121.23

数字循环

1..5 表示从15 ,原型number..number

对浮点取整数

${123.23?int} 输出 123

给变量默认值

${var?default("hello world")?html} 如果var is null 那么将会被hello world 替代

判断对象是不是 null

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23if%20mouse%3Fexists%3E%0A%20%20%20%20%20%20Mouse%20found%0A%3C%23else%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#if mouse?exists>
  2. Mouse found
  3. <#else>
<#if mouse?exists>
      Mouse found
<#else>

也可以直接${mouse?if_exists})输出布尔形

 

--------------------------------------------

(1)解决输出中文乱码问题:

freemarker乱码的原因:

  • 没有使用正确的编码格式读取模版文件,表现为模版中的中文为乱码

解决方法:在classpath上放置一个文件freemarker.properties,在里面写上模版文件的编码方式,比如

default_encoding=UTF-8
locale=zh_CN

注意:eclipse中除了xml文件、java文件外,默认的文件格式iso8859-1

  • 数据插入模版时,没有使用正确的编码,表现出模版中的新插入数据为乱码

解决方法:在result的配置中,指定charset,s2的FreemarkerResult.java会将charset传递freemarker

<action name="ListPersons" class="ListPersons">

<result type="freemarker">

<param name="location">/pages/Person/view.ftl</param>

<param name="contentType"> text/html;charset=UTF-8

</param>

</result>

</action>

(2)提高freemarker的性能

在freemarker.properties中设置:

template_update_delay=60000

避免每次请求都重新载入模版,即充分利用cached的模版

(3)尽量使用freemarker本身的提供的tag,使用S2 tags 的标签会在性能上有所损失

 

(4)freemarker的标签种类:

  • ${..}:FreeMarker will replace it in the output with the actual value of the thing in the curly brackets. They are called interpolation s.
  • # ,代表是FTL tags(FreeMarker Template Language tags) ,hey are instructions to FreeMarker and will not be printed to the output
    • <#if ...></#if>
    • <#list totalList as elementObject>...</#list>
  • @ ,代表用户自定义的标签
  • <#-- --> 注释标签,注意不是<!-- -->

(5)一些特殊的指令:

  • r代表原样输出:${r"C:\foo\bar"}
  • <#list ["winter", "spring", "summer", "autumn"] as x>${x}</#list>
  • ?引出内置指令
    • String处理指令:
      • html:特殊的html字符将会被转义,比如"<",处理后的结果是&lt;
      • cap_first lower_case upper_case
      • trim :除去字符串前后的空格
    • sequences处理指令
      • size :返回sequences的大小
    • numbers处理指令
      • int:number的整数部分,(e.g. -1.9?int is -1)

(6)对于null,或者miss value,freemarker会报错

  • ?exists:旧版本的用法
  • !:default value operator,语法结构为: unsafe_expr !default_expr,比如 ${mouse!"No mouse."} 当mouse不存在时,返回default value;
    • (product.color)!"red" 这种方式,能够处理product或者color为miss value的情况;
    • 而product.color!"red"将只处理color为miss value的情况
  • ??: Missing value test operator ,测试是否为missing value
    • unsafe_expr ?? :product.color??将只测试color是否为null
    • (unsafe_expr )??:(product.color)??将测试product和color是否存在null
Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23if%20mouse%3F%3F%3E%0A%20%20Mouse%20found%0A%3C%23else%3E%0A%20%20No%20mouse%20found%0A%3C%2F%23if%3E%0ACreating%20mouse...%0A%3C%23assign%20mouse%20%3D%20%22Jerry%22%3E%0A%3C%23if%20mouse%3F%3F%3E%0A%20%20Mouse%20found%0A%0A%3C%23else%3E%0A%20%20No%20mouse%20found%0A%3C%2F%23if%3E" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#if mouse??>
  2. Mouse found
  3. <#else>
  4. No mouse found
  5. </#if>
  6. Creating mouse...
  7. <#assign mouse = "Jerry" >
  8. <#if mouse??>
  9. Mouse found
  10. <#else>
  11. No mouse found
  12. </#if>
<#if mouse??>
  Mouse found
<#else>
  No mouse found
</#if>
Creating mouse...
<#assign mouse = "Jerry">
<#if mouse??>
  Mouse found

<#else>
  No mouse found
</#if>

(7)模版值插入方式 (interpolation)

  • 通用方式 Universal interpolations): ${expression }

  • 对于字符串:只是简单输出
  • 对于数值,会自动根据local确定格式,称为human audience,否则称为computer audience,可以"?c", 比如, <a href="/shop/details?id=${product.id ?c }">Details...</a>,因此这里的id是给浏览器使用的,不需要进行格式化,注意?c只对数值有效
  • 对于日期,会使用默认的日期格式转换,因此需要事先设置好默认的转换格式,包括date_format , time_format atetime_format
  • 对于布尔值,不能输出,会报错并停止模版的执行,比如${a = 2} 会出错,但是可以 string built-in来进行转换

数值处理,具体参考:Built-ins for numbers

http://freemarker.org/docs/ref_builtins_number.html#ref_builtin_string_for_number

数值处理的例子:

<#setting number_format="currency"/>
<#assign answer=42/>
${answer}
${answer?string} <#-- the same as ${answer} -->
${answer?string.number}
${answer?string.currency}
${answer?string.percent}

除了使用内置的formate,可以使用任何用Java decimal number format syntax 书写的formate,比如

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

<#setting number_format="0"/>

<#setting number_format="#"/>
${1234}
${12345?string("0.####E0")}

更加方便的格式:

<#setting locale="en_US">
US people writes: ${12345678?string(",##0.00")}

<#setting locale="hu">
Hungarian people writes: ${12345678?string(",##0.00")}



日期处理,参考Built-ins for dates

http://freemarker.org/docs/ref_builtins_date.html#ref_builtin_string_for_date

日期处理的例子:

  • ${openingTime?string.short}
  • ${openingTime?string.medium}
  • ${openingTime?string.long}
  • ${openingTime?string.full}
  • ${nextDiscountDay?string.short}
  • ${nextDiscountDay?string.medium}
  • ${nextDiscountDay?string.long}
  • ${nextDiscountDay?string.full}
  • ${lastUpdated?string.short}
  • ${lastUpdated?string.medium}
  • ${lastUpdated?string.long}
  • ${lastUpdated?string.full}

 

注意:

由于java语言中的Date类型的不足,freemarker不能根据Date变量判断出变量包含的部分(日期、时间还是全部),在这种情况下,freemarker

不能正确显示出${lastUpdated?string.short} 或者 simply ${lastUpdated},因此,可以通过?date, ?time and ?datetime built-ins

来帮助freemarker来进行判断,比如${lastUpdated?datetime?string.short}

除了使用内置的日期转换格式外,可以自己指定日期的格式,使用的是Java date format syntax,比如:

  • ${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
  • ${lastUpdated?string("EEE, MMM d, ''yy")}
  • ${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}

数值专用方式 Numerical interpolations):#{expression } or #{expression ; format },这是数值专用的输出方式,但是 最好使用通用方式的string built-in或者number_format 来完成转换,Numerical interpolations方式将会被停用

(8)创建自定义模版

Ftl代码 <embed type="application/x-shockwave-flash" width="14" height="15" src="http://jiangsha.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" flashvars="clipboard=%3C%23macro%20greet%3E%20%20%20%20%20%20%0A%20%20%20%20%20%3Cfont%20size%3D%22%2B2%22%3EHello%20Joe!%3C%2Ffont%3E%0A%3C%2F%23macro%3E%0A" quality="high" allowscriptaccess="always" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
  1. <#macro greet>
  2. <font size="+2" >Hello Joe!</font>
  3. </#macro>
分享到:
评论

相关推荐

    FreeMarker 2.3.18

    - **页面语法**:`Freemarker页面语法.mht`涵盖了控制结构,如条件语句(`&lt;#if&gt;`、`&lt;#else&gt;`)、循环(`&lt;#foreach&gt;`)、以及导入和包含其他模板等功能。 - **基本语法及实例**:`freemarker基本语法及实例.mht`...

    模板:velocity和freemarker的比较

    首先,从功能上来讲,Freemarker 在 View 层提供了 format 日期和数字的功能,这个功能非常贴心,解决了我们在页面上格式化日期或数字的问题。 Velocity 也提供了类似的功能,但是 Freemarker 的实现更加完善。 ...

    freeMarker语法

    **二、FreeMarker语法** 1. **变量(Variables)**: 变量在模板中用 `${...}` 表示,如 `${name}`,它会从数据模型中查找对应的值。 2. **指令(Directives)**: 指令用于控制模板的结构和行为,如 `#if`, `#...

    freemarker语法.pdf

    ### FreeMarker语法详解 #### 一、FreeMarker模板文件组成 FreeMarker模板文件与普通的HTML页面相比,并不显得更为复杂。其主要组成部分包括: 1. **文本**:这部分内容会被直接输出到最终生成的文档中。 2. **...

    freemarker(FTL)常见语法大全

    ### Freemarker(FTL)常见语法详解 Freemarker是一种基于模板引擎技术的软件,用于生成文本输出。它被广泛应用于Web开发中,用来生成HTML页面或其他格式的文档。Freemarker使用简单直观的语法,使得开发者能够轻松...

    FreeMarker模板基本语法

    **FreeMarker**是一种强大的、基于Java的模板引擎,主要用于动态生成HTML页面,但也支持其他类型的文本格式(如XML、JavaScript、电子邮件等)。它通过将数据模型与表现层分离的方式,使得Web应用程序更加模块化且...

    freemarker(语法+实例)

    Freemarker是一个强大的Java模板引擎,它允许开发者将业务逻辑与视图层分离,使得HTML、XML等静态页面可以通过模板动态生成。以下是关于Freemarker语法和实例的详细讲解: 1. **概念** - **Sequence**:序列,类似...

    FreeMarker语法参考

    ### FreeMarker语法参考详解 #### 一、FreeMarker概述与基本语法 ...以上是对FreeMarker部分核心语法的总结和示例,通过这些基础操作,开发者能够灵活地处理数据,实现动态页面的生成,提升Web应用的效率和用户体验。

    Freemarker常用语法

    Freemarker是一种强大的模板引擎,广泛应用于Web开发中动态页面的生成。其语法灵活且功能丰富,能够处理各种数据类型并支持复杂的逻辑控制结构。以下是对Freemarker常用语法的详细解析,涵盖输出、条件判断、循环、...

    java freemarker 语法

    Freemarker 是一种用于生成动态内容的模板引擎,它能够帮助开发者将数据模型中的数据渲染到 HTML 页面上,从而实现前后端分离的效果。Freemarker 具有良好的可扩展性、灵活性以及强大的功能支持,使得其在 Web 开发...

    Freemarker语法总结

    Freemarker语法总结 Freemarker是一种基于模板的模板引擎,主要用来生成静态 HTML 页面。FreeMarker 模板文件由四个部分组成:文本、注释、插值和 FTL 指令。 一、文本 文本是 Freemarker 模板文件的直接输出部分...

    可视化div布局 生成freemarker模板

    然后在FreeMarker模板中引用这些静态资源,使用FTL语法将动态数据与布局结合。Spring MVC控制器接收请求,处理业务逻辑,构建数据模型,最后将模型传递给FreeMarker模板进行渲染。 7. **最佳实践**:为了提高效率和...

    FreeMarker基础语法

    下面将详细介绍FreeMarker的基础语法及其相关实例。 一、变量与表达式 在FreeMarker模板中,数据模型中的变量可以通过`${}`表达式来访问。例如,如果在Java中传递了一个名为"user"的对象,可以使用`${user.name}`...

    FreeMarker语法知识

    ### FreeMarker语法知识详解 #### 一、FreeMarker概述 FreeMarker是一种强大的模板引擎,用于生成文本输出。它被广泛应用于Web开发中,用于动态生成HTML页面以及其他类型的文本文件,如XML、JavaScript、电子邮件等...

    freemarker 自定义freeMarker标签

    首先,理解FreeMarker的默认标签语法至关重要。FreeMarker使用${...}表达式来插入变量,#{...}用于输出注释,以及、等控制结构进行条件判断和循环。然而,这些默认标签可能无法满足所有复杂的场景,因此自定义标签就...

    freemarker学习资料汇总

    Freemarker是一个强大的Java模板引擎,常用于生成动态HTML、XML等Web页面,尤其是在MVC架构中,作为视图层的技术选型。本压缩包集合了丰富的Freemarker学习资源,涵盖从基础到进阶的各种知识点,以下是这些文档中...

    freemarker 源码、中文API、 freemarker Myeclipse 编辑器

    例如,Freemarker支持控制结构(如if、foreach)、变量表达式、函数调用等,这些都是构建动态页面的基础。同时,手册还会详细介绍如何配置和优化Freemarker以适应你的项目需求。 其次,`freemarker 插件 安装提示....

    SpringBoot+JPA+Freemarker 生成动态SQL

    在这个模板中,你可以用${...}来引用Java对象的属性,使用Freemarker语法构造动态SQL。 5. **编写Service**:创建一个Service类,使用@Autowired注入Repository,然后编写一个方法来处理业务逻辑。在这个方法中,...

Global site tag (gtag.js) - Google Analytics