FreeMarker基本语法

来源:互联网 发布:淘宝网页无法登陆 编辑:程序博客网 时间:2024/05/02 00:05
  1. EG.一个对象BOOK   
  2.  
  3.  
  4. 1.输出 ${book.name}  
  5.  
  6. 空值判断:${book.name?if_exists },  
  7.  
  8. ${book.name?default(‘xxx’)}//默认值xxx  
  9. ${ book.name!"xxx"}//默认值xxx  
  10.  
  11. 日期格式:${book.date?string('yyyy-MM-dd')}  
  12.  
  13. 数字格式:${book?string.number}--20  
  14. ${book?string.currency}--<#-- $20.00 -->  
  15. ${book?string.percent}—<#-- 20% -->  
  16.  
  17. 插入布尔值:  
  18. <#assign foo=ture />  
  19. ${foo?string("yes","no")} <#-- yes -->  
  20.  
  21.  
  22. 2.逻辑判断  
  23. a:  
  24. <#if condition>...  
  25. <#elseif condition2>...  
  26. <#elseif condition3>......  
  27. <#else>...  
  28. 其中空值判断可以写成<#if book.name?? >  
  29.  
  30. </#if>  
  31.  
  32.  
  33. b:  
  34. <#switch value>  
  35. <#case refValue1>  
  36.     ...  
  37.     <#break>  
  38. <#case refValue2>  
  39.     ...  
  40.     <#break>  
  41. ...  
  42. <#case refValueN>  
  43.     ...  
  44.     <#break>  
  45. <#default>  
  46.     ...  
  47. </#switch>  
  48.  
  49. 3.循环读取  
  50. <#list sequence as item>  
  51. ...  
  52. </#list>  
  53. 空值判断<#if bookList?size = 0></#list>  
  54.  
  55.  
  56. e.g.  
  57. <#list employees as e>  
  58. ${e_index}. ${e.name}  
  59. </#list>  
  60. 输出:  
  61. 1. Readonly  
  62. 2. Robbin  
  63.  
  64.  
  65. 4.FreeMarker  
  66.  
  67.  
  68. 3 宏/模板  
  69.  
  70. 宏Macro  
  71.  
  72. 宏是在模板中使用macro指令定义  
  73. l.1 基本用法  
  74. 宏是和某个变量关联的模板片断,以便在模板中通过用户定义指令使用该变量,下面是一个例子:  
  75.  
  76. <#macro greet>  
  77. <font size="+2">Hello Joe!</font>  
  78. </#macro>  
  79.  
  80. 调用宏时,与使用FreeMarker的其他指令类似,  
  81. 只是使用@替代FTL标记中的#。  
  82.  
  83. <@greet></@greet >  
  84.  
  85. <#--<@greet/>-->  
  86.  
  87.  
  88. 在macro指令中可以在宏变量之后定义参数,如:  
  89.  
  90. <#macro greet person>  
  91. <font size="+2">Hello ${person}!</font>  
  92. </#macro>  
  93. 可以这样使用这个宏变量:  
  94.    <@greet person="Fred"/>  
  95. 但是下面的代码具有不同的意思:  
  96.    <@greet person=Fred/>  
  97. 这意味着将Fred变量的值传给person参数,该值不仅是字符串,还可以是其它类型,甚至是复杂的表达式。  
  98.  
  99.  
  100. 宏可以有多参数,下面是一个例子:  
  101. <#macro greet person color>  
  102. <font size="+2" color="${color}">Hello ${person}!</font>  
  103. </#macro>  
  104.  
  105. 可以这样使用该宏变量,其中参数的次序是无关的:  
  106. <@greet person="Fred" color="black"/>  
  107.  
  108. 可以在定义参数时指定缺省值,否则,在调用宏的时候,必须对所有参数赋值:  
  109. <#macro greet person color="black">  
  110. <font size="+2" color="${color}">Hello ${person}!</font>  
  111. </#macro>  
  112.  
  113. 注意:宏的参数是局部变量,只能在宏定义中有效。  
  114.  
  115.    
  116.  
  117.  
  118. 在宏里嵌套内容  
  119. FreeMarker的宏可以有嵌套内容,  
  120. <#nested>指令会执行宏调用指令开始和结束标记之间的模板片断,举一个简单的例子:  
  121.  
  122. <#macro border>  
  123. <table border=4 cellspacing=0 cellpadding=4><tr><td>  
  124.     <#nested>  
  125. </tr></td></table>  
  126. </#macro>  
  127.  
  128. 执行宏调用:  
  129. <@border>The bordered text</@border >  
  130. 输出结果:  
  131. <table border=4 cellspacing=0 cellpadding=4>  
  132. <tr><td>  
  133.        The bordered text  
  134. </tr></td>  
  135. </table>  
  136.  
  137. <#nested>指令可以被多次调用,每次都会执行相同的内容。  
  138. <#macro do_thrice>  
  139. <#nested>  
  140. <#nested>  
  141. <#nested>  
  142. </#macro>  
  143.  
  144.  
  145. <@do_thrice>  
  146. Anything.  
  147. </@do_thrice >  
  148.  
  149. FMPP 输出结果:  
  150. Anything.  
  151. Anything.  
  152. Anything.  
  153.  
  154. 嵌套内容可以是有效的FTL,下面是一个有些复杂的例子,我们将上面三个宏组合起来:  
  155. <@border>  
  156. <ul>  
  157. <@do_thrice>  
  158.     <li><@greet person="Joe"/>  
  159. </@do_thrice >  
  160. </ul>  
  161. </@border >  
  162. 输出结果:  
  163. <table border=4 cellspacing=0 cellpadding=4><tr><td>  
  164. <ul>  
  165.     <li><font size="+2">Hello Joe!</font>  
  166.     <li><font size="+2">Hello Joe!</font>  
  167.     <li><font size="+2">Hello Joe!</font>  
  168. </ul>  
  169. </tr></td></table>  
  170.  
  171. 宏定义中的局部变量对嵌套内容是不可见的,例如:  
  172. <#macro repeat count>  
  173. <#local y = "test">  
  174. <#list 1..count as x>  
  175.     ${y} ${count}/${x}: <#nested>  
  176. </#list>  
  177. </#macro>  
  178.  
  179. <@repeat count=3>${y?default("?")} ${x?default("?")} ${count?default("?")}</@repeat >  
  180.  
  181. 输出结果:  
  182. test 3/1: ? ? ?  
  183. test 3/2: ? ? ?  
  184. test 3/3: ? ? ?  
  185.  
  186.  
  187. 在宏定义中使用循环变量  
  188. nestted指令也可以有循环变量(循环变量的含义见下节),调用宏的时候在宏指令的参数后面依次列出循环变量的名字,格式如下:  
  189.  
  190. <@ macro_name paramter list; loop variable list[,]>  
  191.  
  192. 例如:  
  193. <#macro repeat count>  
  194. <#list 1..count as x>  
  195.     <#nested x, x/2, x==count>  
  196. </#list>  
  197. </#macro>  
  198.  
  199. <@repeat count=4 ; c, halfc, last>  
  200. ${c}. ${halfc}<#if lastLast!</#if>  
  201. </@repeat >  
  202.  
  203. 这里count是宏的参数,c, halfc,last则为循环变量,输出结果:  
  204. 1. 0.5  
  205. 2. 1  
  206. 3. 1.5  
  207. 4. 2 Last!  
  208. 循环变量和宏标记指定的不同不会有问题,如果调用时少指定了循环变量,那么多余的值不可见。调用时多指定了循环变量,多余的循环变量  
  209.  
  210. 不会被创建:  
  211. <@repeat count=4 ; c, halfc, last>  
  212. ${c}. ${halfc}<#if lastLast!</#if>  
  213. </@repeat >  
  214.  
  215. <@repeat count=4 ; c, halfc>  
  216. ${c}. ${halfc}  
  217. </@repeat >  
  218.  
  219. <@repeat count=4>  
  220. Just repeat it...  
  221. </@repeat >  
  222.  
  223. 在模板中定义变量  
  224. 在模板中定义的变量有三种类型:  
  225. plain变量:可以在模板的任何地方访问,包括使用include指令插入的模板,使用assign指令创建和替换。  
  226. 局部变量:在宏定义体中有效,使用local指令创建和替换。  
  227. 循环变量:只能存在于指令的嵌套内容,由指令(如list)自动创建;宏的参数是局部变量,而不是循环变量  
  228. 局部变量隐藏(而不是覆盖)同名的plain变量;循环变量隐藏同名的局部变量和plain变量,下面是一个例子:  
  229.  
  230. <#assign x = "plain">  
  231.  
  232. ${x} <#-- we see the plain var. here -->  
  233.  
  234. <@test/>  
  235. 6. ${x} <#-- the value of plain var. was not changed -->  
  236. <#list ["loop"as x>  
  237. 7. ${x} <#-- now the loop var. hides the plain var. -->  
  238.     <#assign x = "plain2"> <#-- replace the plain var, hiding does not mater here -->  
  239. 8. ${x} <#-- it still hides the plain var. -->  
  240. </#list>  
  241. 9. ${x} <#-- the new value of plain var. -->  
  242. <#macro test>  
  243. 2. ${x} <#-- we still see the plain var. here -->  
  244. <#local x = "local">  
  245. 3. ${x} <#-- now the local var. hides it -->  
  246. <#list ["loop"as x>  
  247.     4. ${x} <#-- now the loop var. hides the local var. -->  
  248. </#list>  
  249. 5. ${x} <#-- now we see the local var. again -->  
  250. </#macro>  
  251. 输出结果:  
  252. 1. plain  
  253. 2. plain  
  254. 3. local 
  255. 4. loop  
  256. 5. local 
  257. 6. plain  
  258. 7. loop  
  259. 8. loop  
  260. 9. plain2  
  261. 内部循环变量隐藏同名的外部循环变量,如:  
  262. <#list ["loop 1"as x>  
  263. ${x}  
  264. <#list ["loop 2"as x>  
  265.     ${x}  
  266.     <#list ["loop 3"as x>  
  267.       ${x}  
  268.     </#list>  
  269.     ${x}  
  270. </#list>  
  271. ${x}  
  272. </#list>  
  273. 输出结果:  
  274. loop 1  
  275.     loop 2  
  276.       loop 3  
  277.     loop 2  
  278. loop 1  
  279. 模板中的变量会隐藏(而不是覆盖)数据模型中同名变量,如果需要访问数据模型中的同名变量,使用特殊变量global,下面的例子假设数据  
  280.  
  281. 模型中的user的值是Big Joe:  
  282. <#assign user = "Joe Hider">  
  283. ${user}          <#-- prints: Joe Hider -->  
  284. ${.globals.user} <#-- prints: Big Joe -->  
  285. 名字空间  
  286. 通常情况,只使用一个名字空间,称为主名字空间,但为了创建可重用的宏、变换器或其它变量的集合(通常称库),必须使用多名字空间,  
  287.  
  288. 其目的是防止同名冲突  
  289. 创建库  
  290. 下面是一个创建库的例子(假设保存在lib/my_test.ftl中):  
  291. <#macro copyright date>  
  292. <p>Copyright (C) ${date} Julia Smith. All rights reserved.  
  293. <br>Email: ${mail}</p>  
  294. </#macro>   
  295. <#assign mail = "jsmith@acme.com ">  
  296. 使用import指令导入库到模板中,Freemarker会为导入的库创建新的名字空间,并可以通过import指令中指定的散列变量访问库中的变量:  
  297. <#import "/lib/my_test.ftl" as my>  
  298. <#assign mail="fred@acme.com ">  
  299. <@my.copyright date="1999-2002"/>  
  300. ${my.mail}  
  301. ${mail}  
  302. 输出结果:  
  303. <p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.  
  304. <br>Email: jsmith@acme.com</p >  
  305. jsmith@acme.com   
  306. fred@acme.com   
  307. 可以看到例子中使用的两个同名变量并没有冲突,因为它们位于不同的名字空间。还可以使用assign指令在导入的名字空间中创建或替代变量  
  308.  
  309. ,下面是一个例子:  
  310. <#import "/lib/my_test.ftl" as my>  
  311. ${my.mail}  
  312. <#assign mail="jsmith@other.com " in my>  
  313. ${my.mail}  
  314. 输出结果:  
  315. jsmith@acme.com   
  316. jsmith@other.com   
  317. 数据模型中的变量任何地方都可见,也包括不同的名字空间,下面是修改的库:  
  318. <#macro copyright date>  
  319. <p>Copyright (C) ${date} ${user}. All rights reserved.</p>  
  320. </#macro>  
  321. <#assign mail = "${user}@acme.com ">  
  322. 假设数据模型中的user变量的值是Fred,则下面的代码:  
  323. <#import "/lib/my_test.ftl" as my>  
  324. <@my.copyright date="1999-2002"/>  
  325. ${my.mail}  
  326. 输出结果:  
  327. <p>Copyright (C) 1999-2002 Fred. All rights reserved.</p>Fred@acme.com