tags:

views:

167

answers:

2

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?

+2  A: 

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'&gt;Groovy&lt;/a&gt;
    <p>This is some
      <b>mixed</b> text. For more see the
      <a href='http://groovy.codehaus.org'&gt;Groovy&lt;/a&gt;project
    </p>
    <p>some text</p>
  </body>
</html>
Chris Dail
Thank you very much for that very thorough answer! I haven't had a chance to try it yet, but I assume that will work. This "yield" syntax seems a bit unelegant and crufty though :(
stian
A: 

I think this works better:-

def writer = new StringWriter()  
    def builder = new groovy.xml.MarkupBuilder(writer)
    builder.html {
        head {
            title"Report"
        }
        body {
            h1"XML encoding with Groovy"
            p"this format can be used as an alternative markup to XML"

            // an element with attributes and text content /
            a(href:'http://groovy.codehaus.org', "Groovy")

            // mixed content /
            p() {
                "This is some"
                "mixed"
                "text. For more see the"
                a(href:'http://groovy.codehaus.org', "Groovy")
                "project"
            }
            p "some text"
        }
    }

Result is:-

<html>
  <head>
    <title>Report</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'&gt;Groovy&lt;/a&gt;
    <p>
      <a href='http://groovy.codehaus.org'&gt;Groovy&lt;/a&gt;
    </p>
    <p>some text</p>
  </body>
</html>
mekondelta