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

The Groovy sample page provides an example of how to use the Groovy mixed-content HTML constructor:

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

This, however, does not work for me, I get an error message like:

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

The Groovy sample page states that:

[Note: the syntax in some of these examples is slightly outdated. See Chapter 8 of GINA on average until these examples are updated.]

So my suspicion is that the syntax of the HTML builder has changed, however I don’t have a book, so I can’t check, and I can’t find a suitable example of this work on the Internet. Does anyone know how the syntax should be in Groovy 1.7 and did it get the job done?

+4
3

, . hrefs [] .

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

:

<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>
+8

, : -

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

: -

<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'>Groovy</a>
    <p>
      <a href='http://groovy.codehaus.org'>Groovy</a>
    </p>
    <p>some text</p>
  </body>
</html>
+1

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

, . , - . "a", , , "p" node.

+1
source

Source: https://habr.com/ru/post/1737804/


All Articles