以下是如何使用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中执行,将显示以下输出
<document> <Student> <name> Joe </name> <ID> 1 </ID> <subject> Physics </subject> </Student> </document>
标签:Groovy
相关阅读 >>
更多相关阅读请进入《Groovy》频道 >>

深入理解Java虚拟机 JVM高级特性与实践 周志明 第3版
这是一部从工作原理和工程实践两个维度深入剖析JVM的著作,是计算机领域公认的经典。