Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grails: Call taglib from g:if tag

Tags:

grails

taglib

I have a custom tag lib that returns a Boolean object so that my GSP can decide whether to display a piece of html or not. I would like to use the g:if tag to check this Boolean's value since I also need to check a few other values (that aren't accesible in the taglib). However, I don't know how to actually call the taglib from the tag?

I've tried:

<g:if test="${<custom:tag/> && other.boolean}">

but that throws errors.

I also tried:

<g:if test="<custom:tag/> && ${other.boolean}">

but that throws errors too.

like image 364
Harry Muscle Avatar asked Jan 11 '23 03:01

Harry Muscle


1 Answers

How does the taglib look like? Looking at the usage it should be as below:

class SomeTagLib {
    static namespace = "custom"
    static returnObjectForTags = ['tag']

    def tag = { attrs, body ->
        //returns an object (can be boolean)
    }
}

By default, taglibs would return StreamCharBuffer. If you need an object to be returned, (as in your case to be used as part of conditional statement), I guess you would need returnObjectFromTags as shown above. It specifies which tag deviates from default behavior and returns an object instead.

Also, you should be using the taglib as mentioned by Tim:

<g:if test="${custom.tag() && other.boolean}"> //should be the appropriate way
like image 183
dmahapatro Avatar answered Jan 30 '23 21:01

dmahapatro