Groovy 模板引擎


当前第2页 返回上一页

以下是如何使用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 变量

Groovy 模板引擎

Groovy dsls

Groovy 映射

Groovy 列表

Groovy 条件语句

Groovy 字符串

Groovy 环境

Groovy 方法

Groovy 可选

更多相关阅读请进入《Groovy》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...