freeMaker使用方式總結

  1. 地址。。。。http://liudaoru.iteye.com/blog/315224
  2. l FreeMarker是一個模板引擎,一個基於模板生成文本輸出的通用工具,使用純Java編寫
  3. l FreeMarker被設計用來生成HTML Web頁面,特別是基於MVC模式的應用程序
  4. l 雖然FreeMarker具備一些編程的能力,但一般由Java程序準備要顯示的數據,由FreeMarker生成頁面,經過模板顯示準備的數據(以下圖)
  5. l FreeMarker不是一個Web應用框架,而適合做爲Web應用框架一個組件
  6. l FreeMarker與容器無關,由於它並不知道HTTP或Servlet;FreeMarker一樣能夠應用於非Web應用程序環境
  7. l FreeMarker更適合做爲Model2框架(如Struts)的視圖組件,你也能夠在模板中使用JSP標記庫
  8. l FreeMarker是免費的
  9. 1、通用目標
  10. l 可以生成各類文本:HTML、XML、RTF、Java源代碼等等
  11. l 易於嵌入到你的產品中:輕量級;不須要Servlet環境
  12. l 插件式模板載入器:能夠從任何源載入模板,如本地文件、數據庫等等
  13. l 你能夠按你所需生成文本:保存到本地文件;做爲Email發送;從Web應用程序發送它返回給Web瀏覽器
  14. 2、強大的模板語言
  15. l 全部經常使用的指令:include、if/elseif/else、循環結構
  16. l 在模板中建立和改變變量
  17. l 幾乎在任何地方均可以使用複雜表達式來指定值
  18. l 命名的宏,能夠具備位置參數和嵌套內容
  19. l 名字空間有助於創建和維護可重用的宏庫,或者將一個大工程分紅模塊,而沒必要擔憂名字衝突
  20. l 輸出轉換塊:在嵌套模板片斷生成輸出時,轉換HTML轉義、壓縮、語法高亮等等;你能夠定義本身的轉換
  21. 3、通用數據模型
  22. l FreeMarker不是直接反射到Java對象,Java對象經過插件式對象封裝,以變量方式在模板中顯示
  23. l 你可使用抽象(接口)方式表示對象(JavaBean、XML文檔、SQL查詢結果集等等),告訴模板開發者使用方法,使其不受技術細節的打擾
  24. 4、爲Web準備
  25. l 在模板語言中內建處理典型Web相關任務(如HTML轉義)的結構
  26. l 可以集成到Model2 Web應用框架中做爲JSP的替代
  27. l 支持JSP標記庫
  28. l 爲MVC模式設計:分離可視化設計和應用程序邏輯;分離頁面設計員和程序員
  29. 5、智能的國際化和本地化
  30. l 字符集智能化(內部使用UNICODE)
  31. l 數字格式本地化敏感
  32. l 日期和時間格式本地化敏感
  33. l 非US字符集能夠用做標識(如變量名)
  34. l 多種不一樣語言的相同模板
  35. 6、強大的XML處理能力
  36. l <#recurse> 和<#visit>指令(2.3版本)用於遞歸遍歷XML樹
  37. l 在模板中清楚和直覺的訪問XML對象模型
  38. FreeMarker設計指南(1)
  39. 1、快速入門
  40. 1)模板 + 數據模型 = 輸出
  41. l FreeMarker基於設計者和程序員是具備不一樣專業技能的不一樣個體的觀念
  42. l 他們是分工勞動的:設計者專一於表示——建立HTML文件、圖片、Web頁面的其它可視化方面;程序員建立系統,生成設計頁面要顯示的數據
  43. l 常常會遇到的問題是:在Web頁面(或其它類型的文檔)中顯示的信息在設計頁面時是無效的,是基於動態數據的
  44. l 在這裏,你能夠在HTML(或其它要輸出的文本)中加入一些特定指令,FreeMarker會在輸出頁面給最終用戶時,用適當的數據替代這些代碼
  45. l 下面是一個例子:
  46. <html>
  47. <head>
  48. <title>Welcome!</title>
  49. </head>
  50. <body>
  51. <h1>Welcome ${user}!</h1>
  52. <p>Our latest product:
  53. <a href="${latestProduct.url}">${latestProduct.name}</a>!
  54. </body>
  55. </html>
  56. l 這個例子是在簡單的HTML中加入了一些由${…}包圍的特定代碼,這些特定代碼是FreeMarker的指令,而包含FreeMarker的指令的文件就稱爲模板(Template)
  57. l 至於user、latestProduct.url和latestProduct.name來自於數據模型(data model)
  58. l 數據模型由程序員編程來建立,向模板提供變化的信息,這些信息來自於數據庫、文件,甚至於在程序中直接生成
  59. l 模板設計者不關心數據從那兒來,只知道使用已經創建的數據模型
  60. l 下面是一個可能的數據模型:
  61. (root)
  62. |
  63. +- user = "Big Joe"
  64. |
  65. +- latestProduct
  66. |
  67. +- url = "products/greenmouse.html"
  68. |
  69. +- name = "green mouse"
  70. l 數據模型相似於計算機的文件系統,latestProduct能夠看做是目錄,而user、url和name看做是文件,url和name文件位於latestProduct目錄中(這只是一個比喻,實際並不存在)
  71. l 當FreeMarker將上面的數據模型合併到模板中,就建立了下面的輸出:
  72. <html>
  73. <head>
  74. <title>Welcome!</title>
  75. </head>
  76. <body>
  77. <h1>Welcome Big Joe!</h1>
  78. <p>Our latest product:
  79. <a href="products/greenmouse.html">green mouse</a>!
  80. </body>
  81. </html>
  82. 2)數據模型
  83. l 典型的數據模型是樹型結構,能夠任意複雜和深層次,以下面的例子:
  84. (root)
  85. |
  86. +- animals
  87. | |
  88. | +- mouse
  89. | | |
  90. | | +- size = "small"
  91. | | |
  92. | | +- price = 50
  93. | |
  94. | +- elephant
  95. | | |
  96. | | +- size = "large"
  97. | | |
  98. | | +- price = 5000
  99. | |
  100. | +- python
  101. | |
  102. | +- size = "medium"
  103. | |
  104. | +- price = 4999
  105. |
  106. +- test = "It is a test"
  107. |
  108. +- whatnot
  109. |
  110. +- because = "don't know"
  111. l 相似於目錄的變量稱爲hashes,包含保存下級變量的惟一的查詢名字
  112. l 相似於文件的變量稱爲scalars,保存單值
  113. l scalars保存的值有兩種類型:字符串(用引號括起,能夠是單引號或雙引號)和數字(不要用引號將數字括起,這會做爲字符串處理)
  114. l 對scalars的訪問從root開始,各部分用「.」分隔,如animals.mouse.price
  115. l 另一種變量是sequences,和hashes相似,只是不使用變量名字,而使用數字索引,以下面的例子:
  116. (root)
  117. |
  118. +- animals
  119. | |
  120. | +- (1st)
  121. | | |
  122. | | +- name = "mouse"
  123. | | |
  124. | | +- size = "small"
  125. | | |
  126. | | +- price = 50
  127. | |
  128. | +- (2nd)
  129. | | |
  130. | | +- name = "elephant"
  131. | | |
  132. | | +- size = "large"
  133. | | |
  134. | | +- price = 5000
  135. | |
  136. | +- (3rd)
  137. | |
  138. | +- name = "python"
  139. | |
  140. | +- size = "medium"
  141. | |
  142. | +- price = 4999
  143. |
  144. +- whatnot
  145. |
  146. +- fruits
  147. |
  148. +- (1st) = "orange"
  149. |
  150. +- (2nd) = "banana"
  151. l 這種對scalars的訪問使用索引,如animals[0].name
  152. 3)模板
  153. l 在FreeMarker模板中能夠包括下面三種特定部分:
  154. Ø ${…}:稱爲interpolations,FreeMarker會在輸出時用實際值進行替代
  155. Ø FTL標記(FreeMarker模板語言標記):相似於HTML標記,爲了與HTML標記區分,用#開始(有些以@開始 ,在後面敘述)
  156. Ø 註釋:包含在<#--和-->(而不是<!--和-->)之間
  157. l 下面是一些使用指令的例子:
  158. Ø if指令
  159. <#if animals.python.price < animals.elephant.price>
  160. Pythons are cheaper than elephants today.
  161. <#else>
  162. Pythons are not cheaper than elephants today.
  163. </#if>
  164. Ø list指令
  165. <p>We have these animals:
  166. <table border=1>
  167. <tr><th>Name<th>Price
  168. <#list animals as being>
  169. <tr><td>${being.name}<td>${being.price} Euros
  170. </#list>
  171. </table>
  172. 輸出爲:
  173. <p>We have these animals:
  174. <table border=1>
  175. <tr><th>Name<th>Price
  176. <tr><td>mouse<td>50 Euros
  177. <tr><td>elephant<td>5000 Euros
  178. <tr><td>python<td>4999 Euros
  179. </table>
  180. Ø include指令
  181. <html>
  182. <head>
  183. <title>Test page</title>
  184. </head>
  185. <body>
  186. <h1>Test page</h1>
  187. <p>Blah blah...
  188. <#include "/copyright_footer.html">
  189. </body>
  190. </html>
  191. Ø 一塊兒使用指令
  192. <p>We have these animals:
  193. <table border=1>
  194. <tr><th>Name<th>Price
  195. <#list animals as being>
  196. <tr>
  197. <td>
  198. <#if being.size = "large"><b></#if>
  199. ${being.name}
  200. <#if being.size = "large"></b></#if>
  201. <td>${being.price} Euros
  202. </#list>
  203. </table>
  204. FreeMarker設計指南(3)
  205. 3、模板
  206. 1)總體結構
  207. l 模板使用FTL(FreeMarker模板語言)編寫,是下面各部分的一個組合:
  208. Ø 文本:直接輸出
  209. Ø Interpolation:由${和},或#{和}來限定,計算值替代輸出
  210. Ø FTL標記:FreeMarker指令,和HTML標記相似,名字前加#予以區分,不會輸出
  211. Ø 註釋:由<#--和-->限定,不會輸出
  212. l 下面是以一個具體模板例子:
  213. <html>[BR]
  214. <head>[BR]
  215. <title>Welcome!</title>[BR]
  216. </head>[BR]
  217. <body>[BR]
  218. <#-- Greet the user with his/her name -->[BR]
  219. <h1>Welcome ${user}!</h1>[BR]
  220. <p>We have these animals:[BR]
  221. <ul>[BR]
  222. <#list animals as being>[BR]
  223. <li>${being.name} for ${being.price} Euros[BR]
  224. </#list>[BR]
  225. </ul>[BR]
  226. </body>[BR]
  227. </html>
  228. l [BR]是用於換行的特殊字符序列
  229. l 注意事項:
  230. Ø FTL區分大小寫,因此list是正確的FTL指令,而List不是;${name}和${NAME}是不一樣的
  231. Ø Interpolation只能在文本中使用
  232. Ø FTL標記不能位於另外一個FTL標記內部,例如:
  233. <#if <#include 'foo'>='bar'>...</if>
  234. Ø 註釋能夠位於FTL標記和Interpolation內部,以下面的例子:
  235. <h1>Welcome ${user <#-- The name of user -->}!</h1>[BR]
  236. <p>We have these animals:[BR]
  237. <ul>[BR]
  238. <#list <#-- some comment... --> animals as <#-- again... --> being>[BR]
  239. ...
  240. Ø 多餘的空白字符會在模板輸出時移除
  241. 2)指令
  242. l 在FreeMarker中,使用FTL標記引用指令
  243. l 有三種FTL標記,這和HTML標記是相似的:
  244. Ø 開始標記:<#directivename parameters>
  245. Ø 結束標記:</#directivename>
  246. Ø 空內容指令標記:<#directivename parameters/>
  247. l 有兩種類型的指令:預約義指令和用戶定義指令
  248. l 用戶定義指令要使用@替換#,如<@mydirective>...</@mydirective>(會在後面講述)
  249. l FTL標記不可以交叉,而應該正確的嵌套,以下面的代碼是錯誤的:
  250. <ul>
  251. <#list animals as being>
  252. <li>${being.name} for ${being.price} Euros
  253. <#if use = "Big Joe">
  254. (except for you)
  255. </#list>
  256. </#if> <#-- WRONG! -->
  257. </ul>
  258. l 若是使用不存在的指令,FreeMarker不會使用模板輸出,而是產生一個錯誤消息
  259. l FreeMarker會忽略FTL標記中的空白字符,以下面的例子:
  260. <#list[BR]
  261. animals as[BR]
  262. being[BR]
  263. >[BR]
  264. ${being.name} for ${being.price} Euros[BR]
  265. </#list >
  266. l 可是,<、</和指令之間不容許有空白字符
  267. 3)表達式
  268. l 直接指定值
  269. Ø 字符串
  270. n 使用單引號或雙引號限定
  271. n 若是包含特殊字符須要轉義,以下面的例子:
  272. ${"It's \"quoted\" and
  273. this is a backslash: \\"}
  274. ${'It\'s "quoted" and
  275. this is a backslash: \\'}
  276. 輸出結果是:
  277. It's "quoted" and
  278. this is a backslash: \
  279. It's "quoted" and
  280. this is a backslash: \
  281. n 下面是支持的轉義序列:
  282. 轉義序列
  283. 含義
  284. \"
  285. 雙引號(u0022)
  286. \'
  287. 單引號(u0027)
  288. \\
  289. 反斜槓(u005C)
  290. \n
  291. 換行(u000A)
  292. \r
  293. Return (u000D)
  294. \t
  295. Tab (u0009)
  296. \b
  297. Backspace (u0008)
  298. \f
  299. Form feed (u000C)
  300. \l
  301. <
  302. \g
  303. >
  304. \a
  305. &
  306. \{
  307. {
  308. \xCode
  309. 416進制Unicode代碼
  310. n 有一類特殊的字符串稱爲raw字符串,被認爲是純文本,其中的\和{等不具備特殊含義,該類字符串在引號前面加r,下面是一個例子:
  311. ${r"${foo}"}
  312. ${r"C:\foo\bar"}
  313. 輸出的結果是:
  314. ${foo}
  315. C:\foo\bar
  316. Ø 數字
  317. n 直接輸入,不須要引號
  318. n 精度數字使用「.」分隔,不能使用分組符號
  319. n 目前版本不支持科學計數法,因此「1E3」是錯誤的
  320. n 不能省略小數點前面的0,因此「.5」是錯誤的
  321. n 數字8、+8088.00都是相同的
  322. Ø 布爾值
  323. n truefalse,不使用引號
  324. Ø 序列
  325. n 由逗號分隔的子變量列表,由方括號限定,下面是一個例子:
  326. <#list ["winter", "spring", "summer", "autumn"] as x>
  327. ${x}
  328. </#list>
  329. 輸出的結果是:
  330. winter
  331. spring
  332. summer
  333. autumn
  334. n 列表的項目是表達式,因此能夠有下面的例子:
  335. [2 + 2, [1, 2, 3, 4], "whatnot"]
  336. n 可使用數字範圍定義數字序列,例如2..5等同於[2, 3, 4, 5],可是更有效率,注意數字範圍沒有方括號
  337. n 能夠定義反遞增的數字範圍,如5..2
  338. Ø 散列(hash)
  339. n 由逗號分隔的鍵/值列表,由大括號限定,鍵和值之間用冒號分隔,下面是一個例子:
  340. {"name":"green mouse", "price":150}
  341. n 鍵和值都是表達式,可是鍵必須是字符串
  342. l 獲取變量
  343. Ø 頂層變量: ${variable},變量名只能是字母、數字、下劃線、$、@和#的組合,且不能以數字開頭
  344. Ø 從散列中獲取數據
  345. n 可使用點語法或方括號語法,假設有下面的數據模型:
  346. (root)
  347. |
  348. +- book
  349. | |
  350. | +- title = "Breeding green mouses"
  351. | |
  352. | +- author
  353. | |
  354. | +- name = "Julia Smith"
  355. | |
  356. | +- info = "Biologist, 1923-1985, Canada"
  357. |
  358. +- test = "title"
  359. 下面都是等價的:
  360. book.author.name
  361. book["author"].name
  362. book.author.["name"]
  363. book["author"]["name"]
  364. n 使用點語法,變量名字有頂層變量同樣的限制,但方括號語法沒有該限制,由於名字是任意表達式的結果
  365. Ø 從序列得到數據:和散列的方括號語法語法同樣,只是方括號中的表達式值必須是數字;注意:第一個項目的索引是0
  366. Ø 序列片段:使用[startIndex..endIndex]語法,從序列中得到序列片段(也是序列);startIndex和endIndex是結果爲數字的表達式
  367. Ø 特殊變量:FreeMarker內定義變量,使用.variablename語法訪問
  368. l 字符串操做
  369. Ø Interpolation(或鏈接操做)
  370. n 可使用${..}(或#{..})在文本部分插入表達式的值,例如:
  371. ${"Hello ${user}!"}
  372. ${"${user}${user}${user}${user}"}
  373. n 可使用+操做符得到一樣的結果
  374. ${"Hello " + user + "!"}
  375. ${user + user + user + user}
  376. n ${..}只能用於文本部分,下面的代碼是錯誤的:
  377. <#if ${isBig}>Wow!</#if>
  378. <#if "${isBig}">Wow!</#if>
  379. 應該寫成:
  380. <#if isBig>Wow!</#if>
  381. Ø 子串
  382. n 例子(假設user的值爲「Big Joe」):
  383. ${user[0]}${user[4]}
  384. ${user[1..4]}
  385. 結果是(注意第一個字符的索引是0):
  386. BJ
  387. ig J
  388. l 序列操做
  389. Ø 鏈接操做:和字符串同樣,使用+,下面是一個例子:
  390. <#list ["Joe", "Fred"] + ["Julia", "Kate"] as user>
  391. - ${user}
  392. </#list>
  393. 輸出結果是:
  394. - Joe
  395. - Fred
  396. - Julia
  397. - Kate
  398. l 散列操做
  399. Ø 鏈接操做:和字符串同樣,使用+,若是具備相同的key,右邊的值替代左邊的值,例如:
  400. <#assign ages = {"Joe":23, "Fred":25} + {"Joe":30, "Julia":18}>
  401. - Joe is ${ages.Joe}
  402. - Fred is ${ages.Fred}
  403. - Julia is ${ages.Julia}
  404. 輸出結果是:
  405. - Joe is 30
  406. - Fred is 25
  407. - Julia is 18
  408. l 算術運算
  409. Ø +、-、×、/、%,下面是一個例子:
  410. ${x * x - 100}
  411. ${x / 2}
  412. ${12 % 10}
  413. 輸出結果是(假設x爲5):
  414. -75
  415. 2.5
  416. 2
  417. Ø 操做符兩邊必須是數字,所以下面的代碼是錯誤的:
  418. ${3 * "5"} <#-- WRONG! -->
  419. Ø 使用+操做符時,若是一邊是數字,一邊是字符串,就會自動將數字轉換爲字符串,例如:
  420. ${3 + "5"}
  421. 輸出結果是:
  422. 35
  423. Ø 使用內建的int(後面講述)得到整數部分,例如:
  424. ${(x/2)?int}
  425. ${1.1?int}
  426. ${1.999?int}
  427. ${-1.1?int}
  428. ${-1.999?int}
  429. 輸出結果是(假設x爲5):
  430. 2
  431. 1
  432. 1
  433. -1
  434. -1
  435. l 比較操做符
  436. Ø 使用=(或==,徹底相等)測試兩個值是否相等,使用!= 測試兩個值是否不相等
  437. Ø =和!=兩邊必須是相同類型的值,不然會產生錯誤,例如<#if 1 = "1">會引發錯誤
  438. Ø Freemarker是精確比較,因此對"x""x ""X"是不相等的
  439. Ø 對數字和日期可使用<、<=、>和>=,但不能用於字符串
  440. Ø 因爲Freemarker會將>解釋成FTL標記的結束字符,因此對於>和>=可使用括號來避免這種狀況,例如<#if (x > y)>
  441. Ø 另外一種替代的方法是,使用lt、lte、gt和gte來替代<、<=、>和>=
  442. l 邏輯操做符
  443. Ø &&(and)、||(or)、!(not),只能用於布爾值,不然會產生錯誤
  444. Ø 例子:
  445. <#if x < 12 && color = "green">
  446. We have less than 12 things, and they are green.
  447. </#if>
  448. <#if !hot> <#-- here hot must be a boolean -->
  449. It's not hot.
  450. </#if>
  451. l 內建函數
  452. Ø 內建函數的用法相似訪問散列的子變量,只是使用「?」替代「.」,下面列出經常使用的一些函數
  453. Ø 字符串使用的:
  454. n html:對字符串進行HTML編碼
  455. n cap_first:使字符串第一個字母大寫
  456. n lower_case:將字符串轉換成小寫
  457. n upper_case:將字符串轉換成大寫
  458. n trim:去掉字符串先後的空白字符
  459. Ø 序列使用的:
  460. n size:得到序列中元素的數目
  461. Ø 數字使用的:
  462. n int:取得數字的整數部分(如-1.9?int的結果是-1
  463. Ø 例子(假設test保存字符串"Tom & Jerry"):
  464. ${test?html}
  465. ${test?upper_case?html}
  466. 輸出結果是:
  467. Tom &amp; Jerry
  468. TOM &amp; JERRY
  469. l 操做符優先順序
  470. 操做符組
  471. 操做符
  472. 後綴
  473. [subvarName] [subStringRange] . (methodParams)
  474. 一元
  475. +expr、-expr、!
  476. 內建
  477. ?
  478. 乘法
  479. *、 / 、%
  480. 加法
  481. +、-
  482. 關係
  483. <、>、<=、>=(lt、lte、gt、gte)
  484. 相等
  485. ==(=)、!=
  486. 邏輯and
  487. &&
  488. 邏輯or
  489. ||
  490. 數字範圍
  491. ..
  492. 4)Interpolation
  493. l Interpolation有兩種類型:
  494. Ø 通用Interpolation:${expr}
  495. Ø 數字Interpolation:#{expr}或#{expr; format}
  496. l 注意:Interpolation只能用於文本部分
  497. l 通用Interpolation
  498. Ø 插入字符串值:直接輸出表達式結果
  499. Ø 插入數字值:根據缺省格式(由#setting指令設置)將表達式結果轉換成文本輸出;可使用內建函數string格式化單個Interpolation,下面是一個例子:
  500. <#setting number_format="currency"/>
  501. <#assign answer=42/>
  502. ${answer}
  503. ${answer?string} <#-- the same as ${answer} -->
  504. ${answer?string.number}
  505. ${answer?string.currency}
  506. ${answer?string.percent}
  507. 輸出結果是:
  508. $42.00
  509. $42.00
  510. 42
  511. $42.00
  512. 4,200%
  513. Ø 插入日期值:根據缺省格式(由#setting指令設置)將表達式結果轉換成文本輸出;可使用內建函數string格式化單個Interpolation,下面是一個使用格式模式的例子:
  514. ${lastUpdated?string("yyyy-MM-dd HH:mm:ss zzzz")}
  515. ${lastUpdated?string("EEE, MMM d, ''yy")}
  516. ${lastUpdated?string("EEEE, MMMM dd, yyyy, hh:mm:ss a '('zzz')'")}
  517. 輸出的結果相似下面的格式:
  518. 2003-04-08 21:24:44 Pacific Daylight Time
  519. Tue, Apr 8, '03
  520. Tuesday, April 08, 2003, 09:24:44 PM (PDT)
  521. Ø 插入布爾值:根據缺省格式(由#setting指令設置)將表達式結果轉換成文本輸出;可使用內建函數string格式化單個Interpolation,下面是一個例子:
  522. <#assign foo=true/>
  523. ${foo?string("yes", "no")}
  524. 輸出結果是:
  525. yes
  526. l 數字Interpolation的#{expr; format}形式能夠用來格式化數字,format能夠是:
  527. Ø mX:小數部分最小X位
  528. Ø MX:小數部分最大X位
  529. Ø 例子:
  530. <#-- If the language is US English the output is: -->
  531. <#assign x=2.582/>
  532. <#assign y=4/>
  533. #{x; M2} <#-- 2.58 -->
  534. #{y; M2} <#-- 4 -->
  535. #{x; m1} <#-- 2.6 -->
  536. #{y; m1} <#-- 4.0 -->
  537. #{x; m1M2} <#-- 2.58 -->
  538. #{y; m1M2} <#-- 4.0 -->
  539. FreeMarker設計指南(4)
  540. 4、雜項
  541. 1)用戶定義指令
  542. l 宏和變換器變量是兩種不一樣類型的用戶定義指令,它們之間的區別是宏是在模板中使用macro指令定義,而變換器是在模板外由程序定義,這裏只介紹宏
  543. l 基本用法
  544. Ø 宏是和某個變量關聯的模板片段,以便在模板中經過用戶定義指令使用該變量,下面是一個例子:
  545. <#macro greet>
  546. <font size="+2">Hello Joe!</font>
  547. </#macro>
  548. Ø 做爲用戶定義指令使用宏變量時,使用@替代FTL標記中的#
  549. <@greet></@greet>
  550. Ø 若是沒有體內容,也可使用:
  551. <@greet/>
  552. l 參數
  553. Ø 在macro指令中能夠在宏變量以後定義參數,如:
  554. <#macro greet person>
  555. <font size="+2">Hello ${person}!</font>
  556. </#macro>
  557. Ø 能夠這樣使用這個宏變量:
  558. <@greet person="Fred"/> and <@greet person="Batman"/>
  559. 輸出結果是:
  560. <font size="+2">Hello Fred!</font>
  561. and <font size="+2">Hello Batman!</font>
  562. Ø 宏的參數是FTL表達式,因此下面的代碼具備不一樣的意思:
  563. <@greet person=Fred/>
  564. Ø 這意味着將Fred變量的值傳給person參數,該值不只是字符串,還能夠是其它類型,甚至是複雜的表達式
  565. Ø 宏能夠有多參數,下面是一個例子:
  566. <#macro greet person color>
  567. <font size="+2" color="${color}">Hello ${person}!</font>
  568. </#macro>
  569. Ø 能夠這樣使用該宏變量:
  570. <@greet person="Fred" color="black"/>
  571. Ø 其中參數的次序是無關的,所以下面是等價的:
  572. <@greet color="black" person="Fred"/>
  573. Ø 只能使用在macro指令中定義的參數,而且對全部參數賦值,因此下面的代碼是錯誤的:
  574. <@greet person="Fred" color="black" background="green"/>
  575. <@greet person="Fred"/>
  576. Ø 能夠在定義參數時指定缺省值,如:
  577. <#macro greet person color="black">
  578. <font size="+2" color="${color}">Hello ${person}!</font>
  579. </#macro>
  580. Ø 這樣<@greet person="Fred"/>就正確了
  581. Ø 宏的參數是局部變量,只能在宏定義中有效
  582. l 嵌套內容
  583. Ø 用戶定義指令能夠有嵌套內容,使用<#nested>指令執行指令開始和結束標記之間的模板片段
  584. Ø 例子:
  585. <#macro border>
  586. <table border=4 cellspacing=0 cellpadding=4><tr><td>
  587. <#nested>
  588. </tr></td></table>
  589. </#macro>
  590. 這樣使用該宏變量:
  591. <@border>The bordered text</@border>
  592. 輸出結果:
  593. <table border=4 cellspacing=0 cellpadding=4><tr><td>
  594. The bordered text
  595. </tr></td></table>
  596. Ø <#nested>指令能夠被屢次調用,例如:
  597. <#macro do_thrice>
  598. <#nested>
  599. <#nested>
  600. <#nested>
  601. </#macro>
  602. <@do_thrice>
  603. Anything.
  604. </@do_thrice>
  605. 輸出結果:
  606. Anything.
  607. Anything.
  608. Anything.
  609. Ø 嵌套內容能夠是有效的FTL,下面是一個有些複雜的例子:
  610. <@border>
  611. <ul>
  612. <@do_thrice>
  613. <li><@greet person="Joe"/>
  614. </@do_thrice>
  615. </ul>
  616. </@border>
  617. 輸出結果:
  618. <table border=4 cellspacing=0 cellpadding=4><tr><td>
  619. <ul>
  620. <li><font size="+2">Hello Joe!</font>
  621. <li><font size="+2">Hello Joe!</font>
  622. <li><font size="+2">Hello Joe!</font>
  623. </ul>
  624. </tr></td></table>
  625. Ø 宏定義中的局部變量對嵌套內容是不可見的,例如:
  626. <#macro repeat count>
  627. <#local y = "test">
  628. <#list 1..count as x>
  629. ${y} ${count}/${x}: <#nested>
  630. </#list>
  631. </#macro>
  632. <@repeat count=3>${y?default("?")} ${x?default("?")} ${count?default("?")}</@repeat>
  633. 輸出結果:
  634. test 3/1: ? ? ?
  635. test 3/2: ? ? ?
  636. test 3/3: ? ? ?
  637. Ø
  638. l 在宏定義中使用循環變量
  639. Ø 用戶定義指令能夠有循環變量,一般用於重複嵌套內容,基本用法是:做爲nested指令的參數傳遞循環變量的實際值,而在調用用戶定義指令時,在<@…>開始標記的參數後面指定循環變量的名字
  640. Ø 例子:
  641. <#macro repeat count>
  642. <#list 1..count as x>
  643. <#nested x, x/2, x==count>
  644. </#list>
  645. </#macro>
  646. <@repeat count=4 ; c, halfc, last>
  647. ${c}. ${halfc}<#if last> Last!</#if>
  648. </@repeat>
  649. 輸出結果:
  650. 1. 0.5
  651. 2. 1
  652. 3. 1.5
  653. 4. 2 Last!
  654. Ø 指定的循環變量的數目和用戶定義指令開始標記指定的不一樣不會有問題
  655. n 調用時少指定循環變量,則多指定的值不可見
  656. n 調用時多指定循環變量,多餘的循環變量不會被建立
  657. 2)在模板中定義變量
  658. l 在模板中定義的變量有三種類型:
  659. Ø plain變量:能夠在模板的任何地方訪問,包括使用include指令插入的模板,使用assign指令建立和替換
  660. Ø 局部變量:在宏定義體中有效,使用local指令建立和替換
  661. Ø 循環變量:只能存在於指令的嵌套內容,由指令(如list)自動建立;宏的參數是局部變量,而不是循環變量
  662. l 局部變量隱藏(而不是覆蓋)同名的plain變量;循環變量隱藏同名的局部變量和plain變量,下面是一個例子:
  663. <#assign x = "plain">
  664. 1. ${x} <#-- we see the plain var. here -->
  665. <@test />
  666. 6. ${x} <#-- the value of plain var. was not changed -->
  667. <#list ["loop"] as x>
  668. 7. ${x} <#-- now the loop var. hides the plain var. -->
  669. <#assign x = "plain2"> <#-- replace the plain var, hiding does not mater here -->
  670. 8. ${x} <#-- it still hides the plain var. -->
  671. </#list>
  672. 9. ${x} <#-- the new value of plain var. -->
  673. <#macro test>
  674. 2. ${x} <#-- we still see the plain var. here -->
  675. <#local x = "local">
  676. 3. ${x} <#-- now the local var. hides it -->
  677. <#list ["loop"] as x>
  678. 4. ${x} <#-- now the loop var. hides the local var. -->
  679. </#list>
  680. 5. ${x} <#-- now we see the local var. again -->
  681. </#macro>
  682. 輸出結果:
  683. 1. plain
  684. 2. plain
  685. 3. local
  686. 4. loop
  687. 5. local
  688. 6. plain
  689. 7. loop
  690. 8. loop
  691. 9. plain2
  692. l 內部循環變量隱藏同名的外部循環變量,如:
  693. <#list ["loop 1"] as x>
  694. ${x}
  695. <#list ["loop 2"] as x>
  696. ${x}
  697. <#list ["loop 3"] as x>
  698. ${x}
  699. </#list>
  700. ${x}
  701. </#list>
  702. ${x}
  703. </#list>
  704. 輸出結果:
  705. loop 1
  706. loop 2
  707. loop 3
  708. loop 2
  709. loop 1
  710. l 模板中的變量會隱藏(而不是覆蓋)數據模型中同名變量,若是須要訪問數據模型中的同名變量,使用特殊變量global,下面的例子假設數據模型中的user的值是Big Joe:
  711. <#assign user = "Joe Hider">
  712. ${user} <#-- prints: Joe Hider -->
  713. ${.globals.user} <#-- prints: Big Joe -->
  714. 3)名字空間
  715. l 一般狀況,只使用一個名字空間,稱爲主名字空間
  716. l 爲了建立可重用的宏、變換器或其它變量的集合(一般稱庫),必須使用多名字空間,其目的是防止同名衝突
  717. l 建立庫
  718. Ø 下面是一個建立庫的例子(假設保存在lib/my_test.ftl中):
  719. <#macro copyright date>
  720. <p>Copyright (C) ${date} Julia Smith. All rights reserved.
  721. <br>Email: ${mail}</p>
  722. </#macro>
  723. <#assign mail = "jsmith@acme.com">
  724. Ø 使用import指令導入庫到模板中,Freemarker會爲導入的庫建立新的名字空間,並能夠經過import指令中指定的散列變量訪問庫中的變量:
  725. <#import "/lib/my_test.ftl" as my>
  726. <#assign mail="fred@acme.com">
  727. <@my .copyright date="1999-2002"/>
  728. ${my.mail}
  729. ${mail}
  730. 輸出結果:
  731. <p>Copyright (C) 1999-2002 Julia Smith. All rights reserved.
  732. <br>Email: jsmith@acme .com</p>
  733. jsmith@acme .com
  734. fred@acme .com
  735. 能夠看到例子中使用的兩個同名變量並無衝突,由於它們位於不一樣的名字空間
  736. l 可使用assign指令在導入的名字空間中建立或替代變量,下面是一個例子:
  737. <#import "/lib/my_test.ftl" as my>
  738. ${my.mail}
  739. <#assign mail="jsmith@other.com" in my>
  740. ${my.mail}
  741. l 輸出結果:
  742. jsmith@acme .com
  743. jsmith@other .com
  744. l 數據模型中的變量任何地方均可見,也包括不一樣的名字空間,下面是修改的庫:
  745. <#macro copyright date>
  746. <p>Copyright (C) ${date} ${user}. All rights reserved.</p>
  747. </#macro>
  748. <#assign mail = "${user}@acme.com">
  749. l 假設數據模型中的user變量的值是Fred,則下面的代碼:
  750. <#import "/lib/my_test.ftl" as my>
  751. <@my .copyright date="1999-2002"/>
  752. ${my.mail}
  753. l 輸出結果:
  754. <p>Copyright (C) 1999-2002 Fred. All rights reserved.</p>
  755. Fred@acme .com
  756. Freemarker - 幾個比較實用的例子 - -
  757. 用Freemarker作模本語言有一段時間了,列出幾個和JSP或者Velocity相比起來比較方便的用途,目的是引誘更多的人跳上Freemarker這個賊船,
  758. 1. String內置的JavaScript轉換: js_string
  759. 用途:用於JavaScript轉義,轉換',",換行等特殊字符
  760. 模板:
  761. <script>
  762. alert("${errorMessage?js_string}");
  763. </script>
  764. 輸出:
  765. <script>
  766. alert("Readonly\'s pet name is \"Cross Bone\"");
  767. </script>
  768. 2.內置的默認值處理:default
  769. 用途: 用於處理默認值
  770. 模本:
  771. User: ${userLogin.name?default("Anonymous")}
  772. <td>${(employee.department.manager.name)?default(" ")}</td>
  773. 輸出:
  774. User: Anonymous
  775. <td> </td>
  776. 注,能夠對整個對象樹加上(),再用內置處理器這種方便的作法,偶也是最近剛學會的,之前一直用很傻的方法作.....
  777. 3. Sequence內置的計數器: xxx_index
  778. 用途:顯示序號
  779. 模板:
  780. <#list employees as e>
  781. ${e_index}. ${e.name}
  782. </#list>
  783. 輸出:
  784. 1. Readonly
  785. 2. Robbin
  786. 4. Sequence內置的分段器: chunk
  787. 用途:某些比較BT的排版需求
  788. 模板:
  789. <#assign seq = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']>
  790. <#list seq?chunk(4) as row>
  791. <ul>
  792. <li><#list row as cell>${cell} </#list></li>
  793. </ul>
  794. </#list>
  795. <#list seq?chunk(4, '-') as row>
  796. <tr>
  797. <td><#list row as cell>${cell} </#list></td>
  798. </tr>
  799. </#list>
  800. 輸出:
  801. <ul>
  802. <li>a</li>
  803. <li>b</li>
  804. <li>c</li>
  805. <li>d</li>
  806. </ul>
  807. <ul>
  808. <li>e</li>
  809. <li>f</li>
  810. <li>g</li>
  811. <li>h</li>
  812. </ul>
  813. <ul>
  814. <li>i</li>
  815. <li>j</li>
  816. </ul>
  817. <tr>
  818. <td>a</td>
  819. <td>b</td>
  820. <td>c</td>
  821. <td>d</td>
  822. </tr>
  823. <tr>
  824. <td>e</td>
  825. <td>f</td>
  826. <td>g</td>
  827. <td>h</td>
  828. </tr>
  829. <tr>
  830. <td>i</td>
  831. <td>j</td>
  832. <td>-</td>
  833. <td>-</td>
  834. </tr>
相關文章
相關標籤/搜索