[groovy-user] Conditional attributes in MarkupBuilder?

186 views
Skip to first unread message

J2R

unread,
Jul 11, 2008, 8:47:32 AM7/11/08
to us...@groovy.codehaus.org

I really like the convenience of the markup builder for constructing HTML.
I'm not sure, though, of the neatest way of adding attributes conditionally
upon the outcome of a test. What I would like to do is simply to omit the
attribute if some condition is met, but I don't know how to do that neatly.
For example, in this code, I'd like to test the value of attrValue and if it
is null, not have the onload attribute at all. Here, though, if it's null, I
end up with <body onload=''> - i.e., an empty attribute rather than omitting
it,

def attrValue="whatever" //may be null

//test here
page.html {

body (onload:attrValue){
}
}

I assume I could do something like the following, but it seems too messy to
be a viable option:

page.html {
if(attrValue){
body (onload:attrValue){
} else {
body (){
}
}
}

Any help would be appreciated!

--
View this message in context: http://www.nabble.com/Conditional-attributes-in-MarkupBuilder--tp18403068p18403068.html
Sent from the groovy - user mailing list archive at Nabble.com.


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

http://xircles.codehaus.org/manage_email


Paul King

unread,
Jul 11, 2008, 6:58:21 PM7/11/08
to us...@groovy.codehaus.org

It looks like you are using MarkupBuilder (not the streaming kind).
In which case, if want that behaviour globally, then you can do:

page.omitNullAttributes = true

before you start using the builder.

Cheers, Paul.

J2R wrote:
> I really like the convenience of the markup builder for constructing HTML.
> I'm not sure, though, of the neatest way of adding attributes conditionally
> upon the outcome of a test. What I would like to do is simply to omit the
> attribute if some condition is met, but I don't know how to do that neatly.
> For example, in this code, I'd like to test the value of attrValue and if it
> is null, not have the onload attribute at all. Here, though, if it's null, I
> end up with <body onload=''> - i.e., an empty attribute rather than omitting
> it,
>
> def attrValue="whatever" //may be null
>
> //test here
> page.html {
>
> body (onload:attrValue){
> }
> }
>
> I assume I could do something like the following, but it seems too messy to
> be a viable option:
>
> page.html {
> if(attrValue){
> body (onload:attrValue){
> } else {
> body (){
> }
> }
> }
>
> Any help would be appreciated!
>

J2R

unread,
Jul 12, 2008, 5:40:34 AM7/12/08
to us...@groovy.codehaus.org


Paul King wrote:
>
>
> It looks like you are using MarkupBuilder (not the streaming kind).
> In which case, if want that behaviour globally, then you can do:
>
> page.omitNullAttributes = true
>
> before you start using the builder.
>

Thanks, yes, that will do the job.
--
View this message in context: http://www.nabble.com/Conditional-attributes-in-MarkupBuilder--tp18403068p18417774.html


Sent from the groovy - user mailing list archive at Nabble.com.

Reply all
Reply to author
Forward
0 new messages