在Groovy 1.7中使用混合内容的HTML生成器的正确语法是什么? [英] What is the correct syntax for using HTML builder with mixed content in Groovy 1.7?

查看:113
本文介绍了在Groovy 1.7中使用混合内容的HTML生成器的正确语法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Groovy示例页面上,有一个如何将Groovy HTML构建器用于混合内容的示例:

  p [
This is some,
bmixed,
text,更多内容请参阅,
ahref:'http://groovy.codehaus.org'[Groovy],
project
]

然而,这并不适用于我,我收到如下错误消息:

 期待']',找到'mixed'@第33行,第23列。bmixed,^ 1错误
$ b

Groovy示例页面指出:

lockquote
<注意:某些语法这些
例子中的一些已经过时了。参见
GINA第8章的平均时间
,直到这些例子被更新。]

我怀疑因此HTML构建器的语法已经改变了,但是我没有这本书,所以我不能检查,我似乎无法找到任何相关的在线工作示例。有没有人知道Groovy 1.7的语法应该如何,并且已经有了这个工作?解决方案

我发现了很多在这个例子中的东西是过时的。混合hrefs的语法和段落周围的[]不适用于我。



对于混合内容,您需要使用特殊关键字'mkp.yield'。如果你不想逃脱,还有一个'mkp.yieldUnescaped'。您还可以使用mkp来完成一些其他功能。



这个例子可以工作并显示使用混合内容:

  def builder = new groovy.xml.MarkupBuilder()
builder.html {
head {
titleGroovy的XML编码

body {
h1使用Groovy进行XML编码
p此格式可用作XML的替代标记

a(href:'http ://groovy.codehaus.org',Groovy)

p {
mkp.yield这是一些
b混合
mkp.yield文本。更多内容请参阅
a(href:'http://groovy.codehaus.org',Groovy)
mkp.yieldproject
}
p一些文本

}

输出:

 < html> 
< head>
< title>使用Groovy< / title>进行XML编码
< / head>
< body>
< h1>使用Groovy进行XML编码< / h1>
< p>此格式可用作XML< / p>的替代标记。
< a href ='http://groovy.codehaus.org'> Groovy< / a>
< p>这是一些
< b>混合< / b>文本。有关更多信息,请参阅
< a href ='http://groovy.codehaus.org'>; Groovy< / a>项目
< / p>
< p>一些文字< / p>
< / body>
< / html>


On the Groovy example page there is an example of how to use Groovy HTML builder with mixed content:

p [
        "This is some",
        b"mixed",
        "text. For more see the",
        ahref:'http://groovy.codehaus.org' ["Groovy"],
        "project"
  ]

This is however not working for me, I get an error message like:

expecting ']', found 'mixed' @ line 33, column 23. b"mixed", ^ 1 error 

The Groovy example page states that:

[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.]

My suspicion therefore is that the syntax of the HTML builder has changed, however I don't have the book so I cannot check and I cannot seem to find any relevant example of this working online. Does anyone know how the syntax is supposed to be in Groovy 1.7 and have got this working?

解决方案

I found a lot of the stuff in that example to be out dated. The syntax for mixed hrefs and the [] around the paragraph do not work for me.

For mixed content, you need to use the special keyword 'mkp.yield'. There is also an 'mkp.yieldUnescaped' if you don't want escaping. There are some other functions you can do with mkp as well.

This example does work and shows using mixed content:

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"    
  } 
}​

Output:

<html>
  <head>
    <title>XML encoding with Groovy</title>
  </head>
  <body>
    <h1>XML encoding with Groovy</h1>
    <p>this format can be used as an alternative markup to XML</p>
    <a href='http://groovy.codehaus.org'>Groovy</a>
    <p>This is some
      <b>mixed</b> text. For more see the
      <a href='http://groovy.codehaus.org'>Groovy</a>project
    </p>
    <p>some text</p>
  </body>
</html>

这篇关于在Groovy 1.7中使用混合内容的HTML生成器的正确语法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆