[groovy-user] MarkupBuilder with dynamic attribute names

709 views
Skip to first unread message

John Hartnup

unread,
Mar 30, 2010, 12:35:15 PM3/30/10
to user
Hi again.

With MarkupBuilder, I can easily create XML nodes by including them as
literals in the Builder invocation:

builder.html {
head { title "My HTML Document" }
}

But what if I want a node with a name that is set at runtime?

def elementName = "title" // could have been input by the user, read
from config, etc.
builder.html {
head {
// how do I cause the builder to output a node named from
elementName here?
}
}

I bet it's obvious, but I've been scratching my head for a while.

Thanks,
John

--
"There is no way to peace; peace is the way"

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Tim Yates

unread,
Mar 30, 2010, 3:15:33 PM3/30/10
to us...@groovy.codehaus.org
You can do this:

import groovy.xml.MarkupBuilder

def elementName = "title" // could have been input by the user, read from config, etc.
def sw = new StringWriter()
new MarkupBuilder( sw ).html {
  head {
    "$elementName" "a title"
  }
}
println sw

Tim

John Hartnup

unread,
Mar 30, 2010, 3:22:19 PM3/30/10
to us...@groovy.codehaus.org
Perfect. Thanks.

virtualeyes

unread,
Jan 28, 2011, 2:15:11 PM1/28/11
to us...@groovy.codehaus.org

Easy enough to interpolate a variable into the builder, but how, if even
possible, would one use a builder in a non-static manner; i.e. one has to
define the start end points (curley braces) in order to get
<tag>content</tag>.

Is there any way to return the start end tags and build (in my case html)
the string dynamically using groovy closure functionality?

example:
import groovy.xml.MarkupBuilder


def sw = new StringWriter()

def build = new MarkupBuilder( sw )

def tags = build.html {} // would like to have here: <html></html>
tags += build.head {}
tags += build.title {"$someTitle"}
if(someCondition) tags += build.specialtag {"$specialness"}
...

and then somehow recurse through the tags using split('><') + inject() to
dynamically create what one would typically do statically with a builder?

Basically I'd like the markup output to be any combination of tags, and not
statically defined.

--
View this message in context: http://groovy.329449.n5.nabble.com/MarkupBuilder-with-dynamic-attribute-names-tp372819p3361981.html
Sent from the groovy - user mailing list archive at Nabble.com.

virtualeyes

unread,
Jan 29, 2011, 3:03:33 PM1/29/11
to us...@groovy.codehaus.org

Actually, with streaming markup builder one can already do this it seems.

Just need to pass builder instance to helper methods outside of the bind
closure; then you can apply conditional logic to build whatever markup that
you need.

Absolutely fantabulous, btw, tagging functionality to build any kind of
markup -- laying concise html templates and forms is cake now ;--)
--
View this message in context: http://groovy.329449.n5.nabble.com/MarkupBuilder-with-dynamic-attribute-names-tp372819p3362927.html

Reply all
Reply to author
Forward
0 new messages