tags:

views:

42

answers:

2

Is there a way to modify the code from a previous answer

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

def awaiting = ['one', 'two', 'three']

builder.html {
    ul {
        awaiting.each { 
            li(it.toString())
        }
    }
}

println writer.toString()

so that if you add a tag instead that you send it - like

    builder.html{
tag{
    awaiting.each{}
    }
} return result

could be 'ol' or 'ul' for example

A: 

You can rely on the GStrings and the fact that you can call a function by its string value.

import groovy.xml.MarkupBuilder

def writer = new StringWriter()
def builder = new MarkupBuilder(writer)

def awaiting = ['one', 'two', 'three']
def tag = 'ol'

builder.html {
    "$tag" {
        awaiting.each { 
            li(it.toString())
        }
    }
}

println writer.toString()
gizmo
Star.. I knew there would be away, but as always first steps seem to take a long time to sink in...
john renfrew
A: 

You could also use invokeMethod which is more explicit then the above, albeit a little bit longer.

builder.html {
  invokeMethod(tag) {
    awaiting.each { 
      li it
} } }
hlg