- Groovy 教程
- Groovy - 首頁
- Groovy - 概述
- Groovy - 環境配置
- Groovy - 基本語法
- Groovy - 資料型別
- Groovy - 變數
- Groovy - 運算子
- Groovy - 迴圈
- Groovy - 條件判斷
- Groovy - 方法
- Groovy - 檔案 I/O
- Groovy - 可選值
- Groovy - 數字
- Groovy - 字串
- Groovy - 範圍
- Groovy - 列表
- Groovy - 對映
- Groovy - 日期和時間
- Groovy - 正則表示式
- Groovy - 異常處理
- Groovy - 面向物件
- Groovy - 泛型
- Groovy - 特性
- Groovy - 閉包
- Groovy - 註解
- Groovy - XML
- Groovy - JMX
- Groovy - JSON
- Groovy - DSL
- Groovy - 資料庫
- Groovy - 構造器
- Groovy - 命令列
- Groovy - 單元測試
- Groovy 模板引擎
- Groovy - 元物件程式設計
- Groovy 有用資源
- Groovy - 快速指南
- Groovy - 有用資源
- Groovy - 討論
Groovy 模板引擎
Groovy 的模板引擎的工作方式類似於郵件合併(為了方便傳送郵件,特別是廣告郵件到多個地址,自動將資料庫中的姓名和地址新增到信件和信封中),但它更加通用。
字串中的簡單模板
在下面的簡單示例中,我們首先定義一個名為 name 的變數來儲存字串“Groovy”。在 println 語句中,我們使用 $ 符號來定義一個引數或模板,可以在其中插入值。
def name = "Groovy"
println "This Tutorial is about ${name}"
如果以上程式碼在 Groovy 中執行,則會顯示以下輸出。輸出清楚地顯示 $name 被 def 語句賦值的值替換了。
簡單模板引擎
以下是一個 SimpleTemplateEngine 的示例,它允許您在模板中使用類似 JSP 的指令碼和小指令碼表示式來生成引數化文字。模板引擎允許您繫結引數列表及其值,以便可以替換包含已定義佔位符的字串。
def text ='This Tutorial focuses on $TutorialName. In this tutorial you will learn about $Topic' def binding = ["TutorialName":"Groovy", "Topic":"Templates"] def engine = new groovy.text.SimpleTemplateEngine() def template = engine.createTemplate(text).make(binding) println template
如果以上程式碼在 Groovy 中執行,則會顯示以下輸出。
現在讓我們將模板功能用於 XML 檔案。第一步,讓我們將以下程式碼新增到名為 Student.template 的檔案中。在下面的檔案中,您會注意到我們沒有新增元素的實際值,而是佔位符。因此,$name、$is 和 $subject 都作為佔位符,需要在執行時替換。
<Student>
<name>${name}</name>
<ID>${id}</ID>
<subject>${subject}</subject>
</Student>
現在讓我們新增 Groovy 指令碼程式碼來新增可以用來替換上述模板的實際值的功能。關於以下程式碼,需要注意以下幾點。
佔位符到實際值的對映是透過繫結和 SimpleTemplateEngine 完成的。繫結是一個 Map,其中佔位符作為鍵,替換值作為值。
import groovy.text.*
import java.io.*
def file = new File("D:/Student.template")
def binding = ['name' : 'Joe', 'id' : 1, 'subject' : 'Physics']
def engine = new SimpleTemplateEngine()
def template = engine.createTemplate(file)
def writable = template.make(binding)
println writable
如果以上程式碼在 Groovy 中執行,則會顯示以下輸出。從輸出可以看出,值已成功替換到相關的佔位符中。
<Student> <name>Joe</name> <ID>1</ID> <subject>Physics</subject> </Student>
StreamingTemplateEngine
StreamingTemplateEngine 引擎是 Groovy 中可用的另一個模板引擎。這與 SimpleTemplateEngine 類似,但使用可寫閉包建立模板,使其更適合大型模板。具體來說,此模板引擎可以處理大於 64k 的字串。
以下是 StreamingTemplateEngine 的使用方法示例:
def text = '''This Tutorial is <% out.print TutorialName %> The Topic name
is ${TopicName}'''
def template = new groovy.text.StreamingTemplateEngine().createTemplate(text)
def binding = [TutorialName : "Groovy", TopicName : "Templates",]
String response = template.make(binding)
println(response)
如果以上程式碼在 Groovy 中執行,則會顯示以下輸出。
This Tutorial is Groovy The Topic name is Templates
XMLTemplateEngine
XmlTemplateEngine 用於模板場景,其中模板源和預期輸出都應為 XML。模板使用正常的 ${expression} 和 $variable 符號將任意表達式插入模板中。
以下是 XMLTemplateEngine 的使用方法示例。
def binding = [StudentName: 'Joe', id: 1, subject: 'Physics']
def engine = new groovy.text.XmlTemplateEngine()
def text = '''\
<document xmlns:gsp='http://groovy.codehaus.org/2005/gsp'>
<Student>
<name>${StudentName}</name>
<ID>${id}</ID>
<subject>${subject}</subject>
</Student>
</document>
'''
def template = engine.createTemplate(text).make(binding)
println template.toString()
如果以上程式碼在 Groovy 中執行,則會顯示以下輸出
Joe
1
Physics