FreeMarker常用语法学习

1.API网址

http://freemarker.sourceforge.net/docs/

2.一个Table的例子

[html]  view plain  copy
  1. freemarker 对表格的控制  
  2. 这里将所有需要在一个区域显示到数据全部add到一个叫做zbj的list中了  
  3. <#assign a2=zbj> -->将list zbj赋值给a2,这里也应该判空的...  
  4. <#if zbj?exists>         
  5.   <#list 0..zbj?size-1 as a1>    -->从0--->zbj?size-1循环,此时可以将数据全部循环一边  
  6.      <#if a1%(4*ldList?size)==0> <tr></#if> -->模板语言支持运算符操作在这里用到了取余判断并写<tr>  
  7.         <td height="23"><input type="text" size="7" id="input1" value=${a2[a1]} name="alldata"/></td> -->写数据  
  8.      </#list>        
  9. </#if>  
  10. freemarker有一个内置函数叫做chunk,没有试出来,才做了这样的处理,应该chunk更能方便到将数据制作成需要到格子吧....  

3.常用语法

[html]  view plain  copy
  1. =======常用语法==========   
  2. 三.   
  3. EG.一个对象BOOK   
  4. 1.输出 ${book.name}   
  5. 空值判断:${book.name?if_exists },   
  6. ${book.name?default(‘xxx’)}//默认值xxx   
  7. ${ book.name!"xxx"}//默认值xxx   
  8. 日期格式:${book.date?string('yyyy-MM-dd')}   
  9. 数字格式:${book?string.number}--20   
  10. ${book?string.currency}--<#-- $20.00 -->   
  11. ${book?string.percent}—<#-- 20% -->   
  12. 插入布尔值:   
  13. <#assign foo=ture />   
  14. ${foo?string("yes","no")} <#-- yes -->  
  15. =============逻辑判断===========   
  16. 2.   
  17. a:   
  18. <#if condition>...   
  19. <#elseif condit   
  20. inc 发布于2007-09-08 16:18:57   
  21. ion2>...   
  22. <#elseif condition3>......   
  23. <#else>...   
  24. 其中空值判断可以写成<#if book.name?? >  
  25. </#if>   
  26. b:   
  27. <#switch value>   
  28. <#case refValue1>   
  29. ...   
  30. <#break>   
  31. <#case refValue2>   
  32. ...   
  33. <#break>   
  34. ...   
  35. <#case refValueN>   
  36. ...   
  37. <#break>   
  38. <#default>   
  39. ...   
  40. </#switch>  
  41.   
  42. ========循环读取==========   
  43. 3.   
  44. <#list sequence as item>   
  45. ...   
  46. </#list>   
  47. 空值判断<#if bookList?size = 0></#list>   
  48. e.g.   
  49. <#list employees as e>   
  50. ${e_index}. ${e.name}   
  51. </#list>  
  52. <#if mole?default(0)!=0>  
  53. =============用来压缩空白空间和空白的行========  
  54. 用例   
  55. <#assign x = " moo ">   
  56. (<#compress>   
  57. 1 2 3 4 5   
  58. ${moo}   
  59. test only  
  60. I said, test only  
  61. )   
  62. 输出   
  63. (1 2 3 4 5   
  64. moo   
  65. test only   
  66. I said, test only)  
  67. =======Sequence内置的计数器=======  
  68. 3. Sequence内置的计数器: xxx_index   
  69. 用途:显示序号   
  70. 模板:  
  71. <#list employees as e>   
  72. ${e_index}. ${e.name}   
  73. </#list>  
  74. 输出:  
  75. 1. Readonly   
  76. 2. Robbin  
  77.   
  78. =======Sequence内置的分段器==============   
  79. 4. Sequence内置的分段器: chunk   
  80. 用途:某些比较BT的排版需求   
  81. 模板:  
  82. <#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']>   
  83. <#list seq?chunk(4) as row>   
  84. <ul>   
  85. <li><#list row as cell>${cell} </#list></li>   
  86. </ul>   
  87. </#list>  
  88. <#list seq?chunk(4, '-') as row>   
  89. <tr>   
  90. <td><#list row as cell>${cell} </#list></td>   
  91. </tr>   
  92. </#list>  
  93.   
  94. 输出:  
  95. <ul>   
  96. <li>a</li>   
  97. <li>b</li>   
  98. <li>c</li>   
  99. <li>d</li>   
  100. </ul>   
  101. <ul>   
  102. <li>e</li>   
  103. <li>f</li>   
  104. <li>g</li>   
  105. <li>h</li>   
  106. </ul>   
  107. <ul>   
  108. <li>i</li>   
  109. <li>j</li>   
  110. </ul>  
  111. <tr>   
  112. <td>a</td>   
  113. <td>b</td>   
  114. <td>c</td>   
  115. <td>d</td>   
  116. </tr>   
  117. <tr>   
  118. <td>e</td>   
  119. <td>f</td>   
  120. <td>g</td>   
  121. <td>h</td>   
  122. </tr>   
  123. <tr>   
  124. <td>i</td>   
  125. <td>j</td>   
  126. <td>-</td>   
  127. <td>-</td>   
  128. </tr>  
  129. ==========.设置缺省格式指令setting ============  
  130. <#setting number_format = "#"/>   
  131. ${1.234}   
  132. 输出1   
  133. <#setting number_format="0.##">   
  134. ${1.234}   
  135. 输出   
  136. 1.23  
  137. ==========包含文件指令 include ============  
  138. <#include "header.htm"/>  
  139. ==========导入macros指令 import =======  
  140. <#import "../macros/pagination.ftl" as pagination>  
  141. ==========freemarker的list============  
  142.   
  143. Scalar String:${scalarString}   
  144. Scalar Number:${scalarNumber}   
  145. Object is:${scalarObject}  
  146. List使用样例-List元素为Scalar对象:  
  147. <#list scalarList as value0>   
  148. Scalar List值:${value0}   
  149. </#list>  
  150. List使用样例-List元素为User对象:  
  151. <#list userList as listUser>   
  152. List对象User Id值:${listUser.userId}   
  153. </#list>  
  154. Map使用样例-Map Values元素为Scalar :  
  155. <#list scalarMap?keys as mykey>   
  156. Scalar Map key is :${mykey}   
  157. Scalar Map value is:${scalarMap[mykey]}   
  158. </#list>  
  159. Map使用样例-Map Values元素为User对象:  
  160. <#list userMap?keys as key1>   
  161. <#assign mapUser="${userMap[key1]}" >   
  162. User Object is :${mapUser}   
  163. <#--   
  164. 以下方法有问题   
  165. User is :${mapUser.userId} <br>   
  166. -->   
  167. </#list>  
  168. =======FreeMarker中list排序=======   
  169. 升序:   
  170. <#list list?sort_by("time") as v>   
  171. </#list>   
  172. 降序:   
  173. <#list list?sort_by("time") as v>   
  174. </#list>  
  175.   
  176. ========freemarker在模板中定义变量=======  
  177. 在模板中定义的变量有三种类型:   
  178. plain变量:可以在模板的任何地方访问,包括include指令插入的模板,使用assign指令创建和替换   
  179. <#include "/WEB-INF/index/top.html">  
  180.   
  181. =======freemarker.properties配置=========   
  182. (1)解决输出中文乱码问题:   
  183. default_encoding=UTF-8   
  184. locale=zh_CN   
  185. number_format=#  
  186. (2)提高freemarker的性能   
  187. template_update_delay=60000  
  188. (3)freemarker的标签种类:   
  189. ${..}   
  190. # 代表是FTL tags   
  191. <#if ...></#if>   
  192. <#list totalList as elementObject>...</#list>   
  193. @ ,代表用户自定义的标签   
  194. <#-- --> 注释标签,注意不是<!-- -->  
  195. ==============将图片整除换行====== <#if (u_index+1)%4=0>当图片超过五个就换行=======   
  196. <table width="100%">   
  197. <tr>   
  198. <td><table width="100%">   
  199. <tr>   
  200. <#if map["最新-图片"]?exists>   
  201. <#list map["最新-图片"] as u>   
  202. <#if (u_index+1)%4=0>   
  203. <td><table width="70" height="65" border="0" cellpadding="0" cellspacing="0">   
  204. <tr>  
  205. <td align="center" valign="middle" bgcolor="#6F7074">   
  206. <a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 "><img src="${u.chartpath?default('')}" width="67" height="41" border="0" /></a></td>   
  207. </tr>   
  208. <tr>   
  209. <td align="center" bgcolor="#6F7074">   
  210. <a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 " class="tableff">   
  211. <@jf.greet title=u.title len=4 /></a>   
  212. </td>   
  213. </tr>  
  214. </table></td></tr><tr >   
  215. <#else>   
  216. <td><table width="70" height="65" border="0" cellpadding="0" cellspacing="0">   
  217. <tr>  
  218. <td align="center" valign="middle" bgcolor="#6F7074">   
  219. <a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 "><img src="${u.chartpath?default('')}" width="67" height="41" border="0" /></a></td>   
  220. </tr>   
  221. <tr>   
  222. <td align="center" bgcolor="#6F7074">   
  223. <a target="_blank" href="shownews.page?id=${u.id?default("")}&mole=2 " class="tableff">   
  224. <@jf.greet title=u.title len=4 /></a>   
  225. </td>   
  226. </tr>  
  227. </table></td>   
  228. </#if>   
  229. </#list>   
  230. </#if>   
  231. </tr>   
  232. </table>   
  233. </td>   
  234. </tr>   
  235. </table>   
  236. </tr>   
  237. </table></td>   
  238. </#if>   
  239. </#list>   
  240. </#if>   
  241. </tr>   
  242. </table>   
  243. </td>   
  244. </tr>   
  245. </table>  
  246. =================freemarker中在application   
  247. ${Application.web_title}  
  248. 在类里:   
  249. list=dser.indexvideo(cvo);   
  250. if(list.size()!=0){   
  251. map.put("最新",list);}  
  252. 在html静态页里:   
  253. <#import "/WEB-INF/ftl/index.ftl" as jf>   
  254. (index.ftl   
  255. <#macro greet title len>   
  256. <#if title?length != 0>   
  257. <#if (title?length>len)>   
  258. ${title[0..len-1]} <#else>   
  259. ${title?trim}   
  260. </#if>   
  261. <#else>   
  262. ${title?default("")}   
  263. </#if>   
  264. </#macro>   
  265. <#--时间比较-->   
  266. <#macro vstime start end>   
  267. <#if start?default("")==""||end?default("")=="">   
  268. --   
  269. <#else>   
  270. <#assign starts=start?replace("-","") >   
  271. <#assign ends=end?replace("-","") >   
  272. <#if (starts?number>ends?number)>   
  273. <font color="red">以过期</font>   
  274. <#else>   
  275. 未过期   
  276. </#if>   
  277. </#if>   
  278. </#macro>   
  279. )  
  280. <#if map["最新"]?exists>   
  281. <#list map["最新"] as u>   
  282. ${u.filepath?default('')}   
  283. <@jf.greet title=u.title len=6 />   
  284. </#list></#if>   

猜你喜欢

转载自blog.csdn.net/scholar_man/article/details/79971357