GroovletMarkupBuilder
Using Markup Builder
Markup Builder is a part of the Groovy standard library and is used to generate XML. It is in the package groovy.xml.
In a Groovlet context a markup builder is bound to the variable html. This markup builder is bound to the response's output stream.
This means we can quickly create XHTML output from Groovlets.
The markup generation should always start: html.html().
However the markup builder is not validating so what we do from here is really down to us. If we construct invalid tags then only the consumer is going to moan.
A Basic Page
A basic page is constructed like this:
html.html() { head() { title('Greetings') } body() { h1('Hello World') p('Greetings!') } }
Nuances
Markup Builder is based on the basic Groovy Builder (a pattern that gets used a lot in Groovy). The builder consists of metaprogrammed functions that will form tags and arguments to those functions.
p('Greetings') becomes <p>Greetings</p>
We can also use properties in these method calls and these will become attributes of the tag.
p(class: red, 'Greetings') becomes <p class="red">Greetings</p>