Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the correct syntax for using HTML builder with mixed content in Groovy 1.7?

Tags:

html

groovy

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?

like image 406
stian Avatar asked Mar 20 '10 12:03

stian


1 Answers

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>
like image 200
Chris Dail Avatar answered Sep 27 '22 21:09

Chris Dail