In oh, so many ways!
Groovy’s MarkupBuilder class can really clean up your code.
import groovy.xml.*
def out = new StringWriter()
def b = new MarkupBuilder(out)
b.table(id: 'test', border: 0) {
tbody {
tr {
td "hello"
td(id: 'hello_id', 'hello')
td(id: 'test', /hello/)
td(id: 'esc') { mkp.yield ' ' }
td(id: 'unesc') { mkp.yieldUnescaped ' ' }
td(id: 999) { mkp.yield 999 }
td(id: 99) { mkp.yieldUnescaped 99 }
td(id: 88) { mkp.yieldUnescaped 'hello' }
td(id: 77) { mkp.yieldUnescaped "hello2" }
td(id: 66) { mkp.yieldUnescaped """hello3""" }
// gives compilation error "unexpected token: } at line: 19, column: 48":
// td(id: 55) { mkp.yieldUnescaped /hello4/ }
td(id: 55) { mkp.yieldUnescaped "" + /hello4/ }
}
}
}
println out.toString()
This produces:
<table id='test' border='0'>
<tbody>
<tr>
<td>hello</td>
<td id='hello_id'>hello</td>
<td id='test'>hello</td>
<td id='esc'> </td>
<td id='unesc'> </td>
<td id='999'>999</td>
<td id='99'>99</td>
<td id='88'>hello</td>
<td id='77'>hello2</td>
<td id='66'>hello3</td>
<td id='55'>hello4</td>
</tr>
</tbody>
</table>
This was just another of those “external brain dump” posts…I didn’t want to loose my memory and there are a few subtle points (or at least a few things that I found out by trial-and-error). There’s not too much other stuff around.
