Groovy模板引擎上(基础模板介绍)
原文链接?作者:groovy团队 ?译者:树下偷懒的蚁 1.简介 2.模板框架 在Groovy中,模板框架包含TemplateEngine抽象基类(引擎必须实现),Template接口(引擎生成的模板必须实现)。
Groovy包含的以下几种模板引擎:
3. SimpleTemplateEngine SimpleTemplateEngine允许在模板中使用类似JSP风格的代码(如下例),脚本和EL表达式。样例 import groovy.text.SimpleTemplateEngine def 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() ? 然而,通常在模板中混入业务逻辑不是良好的习惯。但是有时一些简单的逻辑是有用的。上述的例子中,我们可以修改一下: $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-Dev 5. XmlTemplateEngine XmlTemplateEngine适用于输入模板输出结果都是XML样式的场景。可以在模板的任意表达式中使用${expression} 和 $variable符号。同时也支持特殊的标签: 注解和处理指令在处理的过程中会被移除,同时特殊的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 此模板引擎主要适用于生成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> 此模板引擎的主要特点:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |