Groovy模板引擎上(基礎模板介紹)
原文鏈接 作者:groovy團隊 譯者:樹下偷懶的蟻
1.簡介
Groovy支持多種方式動態的生成文本譬如:GStrings, printf(基于Java5),MarkupBuilder 。除此之外,模板框架則是非常適用基于靜態模板生成文本的應用程序。
2.模板框架
在Groovy中,模板框架包含TemplateEngine抽象基類(引擎必須實現),Template接口(引擎生成的模板必須實現)。
Groovy包含的以下幾種模板引擎:
- SimpleTemplateEngine-基礎模板引擎
- GStringTemplateEngine-將模板作為可寫的閉包 (適用于流操作)
- XmlTemplateEngine-適用于輸出XML格式的模板引擎
- MarkupTemplateEngine– 非常完整優化的模板引擎
3. SimpleTemplateEngine</p>
SimpleTemplateEngine允許在模板中使用類似JSP風格的代碼(如下例),腳本和EL表達式。樣例
import groovy.text.SimpleTemplateEnginedef text = 'Dear "$firstname $lastname",\nSo nice to meet you in <% print city %>.\nSee you in ${month},\n${signed}'
def binding = ["firstname":"Sam", "lastname":"Pullara", "city":"San Francisco", "month":"December", "signed":"Groovy-Dev"]
def engine = new SimpleTemplateEngine() template = engine.createTemplate(text).make(binding)
def result = 'Dear "Sam Pullara",\nSo nice to meet you in San Francisco.\nSee you in December,\nGroovy-Dev'
assert result == template.toString()</pre>
然而,通常在模板中混入業務邏輯不是良好的習慣。但是有時一些簡單的邏輯是有用的。上述的例子中,我們可以修改一下:
$firstname
可以改為(假設模板已經import了capitalize)
${firstname.capitalize()}或者這樣
<% print city %>改為:
<% print city == "New York" ? "The Big Apple" : city %>3.1.高級應用說明
如果直接將模板嵌入到腳本中(如我們上面做的那樣),必須小心反斜杠轉義。模板中的字符串在傳入到模板框架之前需要Groovy解析,而GString表達式以及腳本代碼作為Groovy程序的一部分,必須要轉義反斜杠。例如,想用引號把 The Big Apple引起來,可以這樣做:
<% print city == "New York" ? "\\"The Big Apple\\"" : city %>相似的,如果想新起一行,我們可以這樣用:
\\n“\n” 可以在靜態模板文本中使用,也可以在外部模板文件中使用。同樣,如果要顯示反斜線本身,要用:
\\\\在外部文件中:
\\\\4.GStringTemplateEngine
使用GStringTemplateEngine的方法,和上述的例子有點類似(顯示更多的參數)。首先,我們將模板存在文件中:
test.template
Dear "$firstname $lastname", So nice to meet you in <% out << (city == "New York" ? "\\"The Big Apple\\"" : city) %>. See you in ${month}, ${signed}注意:我們使用out替代print支持GStringTemplateEngine的流特性。因為我們將文件存儲在單獨的文件中,所以不需要轉義反斜線。調用過程如下:
def f = new File('test.template') engine = new GStringTemplateEngine() template = engine.createTemplate(f).make(binding) println template.toString()輸入結果如下:
Dear "Sam Pullara", So nice to meet you in "The Big Apple". See you in December, Groovy-Dev5. XmlTemplateEngine</p>
XmlTemplateEngine適用于輸入模板輸出結果都是XML樣式的場景。可以在模板的任意表達式中使用${expression} 和 $variable符號。同時也支持特殊的標簽:<gsp:scriptlet>(用戶插入代碼片段) and<gsp:expression>(用于輸入結果的代碼片段).
注解和處理指令在處理的過程中會被移除,同時特殊的XML符號比如:<,>,"和'會被相應的XML符號轉義。輸出結果將按照標準的XML輸出格式進行縮進。gsp:tags 定義的Xmlns命名空間會被移除但是其他的命名空間將會被保留(可能轉換成XML樹中等效的結果)。
正常情況下,模板原文件會保存在單獨的文件中,但是下面的例子提供一個String類型的XML模板。
def binding = [firstname: 'Jochen', lastname: 'Theodorou', nickname: 'blackdrag', salutation: 'Dear'] def engine = new groovy.text.XmlTemplateEngine() def text = '''\ <document xmlns:gsp='http://groovy.codehaus.org/2005/gsp' xmlns:foo='baz' type='letter'> <gsp:scriptlet>def greeting = "${salutation}est"</gsp:scriptlet> <gsp:expression>greeting</gsp:expression> <foo:to>$firstname "$nickname" $lastname</foo:to> How are you today? </document> ''' def template = engine.createTemplate(text).make(binding) println template.toString()輸出結果如下:
<document type=’letter’> Dearest <foo:to xmlns:foo=’baz’> Jochen "blackdrag" Theodorou </foo:to> How are you today? </document>
6. The MarkupTemplateEngine</p>
此模板引擎主要適用于生成XML風格類似的標記(XML, XHTML, HTML5, …),但是也可以用于生成任意文本。和傳統的模板引擎不同的是,此模板引擎基于DSL。如下模板樣例:
xmlDeclaration() cars { cars.each { car(make: it.make, model: it.model) } }如果用下面的數據模型填充:
model = [cars: [new Car(make: 'Peugeot', model: '508'), new Car(make: 'Toyota', model: 'Prius')]]渲染的結果:
<?xml version='1.0'?> <cars><car make='Peugeot' model='508'/><car make='Toyota' model='Prius'/></cars>此模板引擎的主要特點:
- 標記構建器風格的語法
- 模板編譯成字節代碼
- 渲染迅速
- 可選擇的模板數據類型校驗
- 包含
- 國際化支持
- 碎片化/布局
原創文章,轉載請注明: 轉載自并發編程網 – ifeve.com
本文鏈接地址: Groovy模板引擎上(基礎模板介紹)