在Groovy example page上有一个如何使用混合内容的Groovy HTML构建器的示例:
p [ "This is some",b"mixed","text. For more see the",ahref:'http://groovy.codehaus.org' ["Groovy"],"project" ]
@H_403_8@然而,这对我不起作用,我收到如下错误消息:
expecting ']',found 'mixed' @ line 33,column 23. b"mixed",^ 1 error
@H_403_8@Groovy示例页面声明:
[Note: the Syntax in some of these
examples is slightly out-dated. See
chapter 8 of GINA in the mean-time
until these examples are updated.]因此我怀疑HTML构建器的语法已经改变,但是我没有这本书所以我无法检查,我似乎无法找到任何相关的在线工作示例.有谁知道Groovy 1.7中的语法应该是如何工作的?
最佳答案
我发现那个例子中有很多东西要过时了.混合href的语法和段落周围的[]对我不起作用.
对于混合内容,您需要使用特殊关键字’mkp.yield’.如果您不想转义,还有一个’mkp.yieldUnescaped’.您还可以使用mkp执行其他一些功能.
def builder = new groovy.xml.MarkupBuilder() builder.html { head { title"XML encoding with Groovy" } body { h1"XML encoding with Groovy" p"this format can be used as an alternative markup to XML" a(href:'http://groovy.codehaus.org',"Groovy") p { mkp.yield "This is some" b"mixed" mkp.yield " text. For more see the" a(href:'http://groovy.codehaus.org',"Groovy") mkp.yield "project" } p "some text" } }
@H_403_8@输出:
@H_403_8@