Test Link

19 / Apr / 2010 by Hitesh Bhatia 1 comments

(based on Tomás Lin’s truthlink)

This is useful when one requires link on certain condition.
Instead of first using condition tag and then tag for link these can be combined into one using taglib.

 
     
	text 
     

or

   
    
     Text 
    

Other way of doing this is by using taglib.

class AbcTagLib {
namespaces "abc";

    def testLink = {attrs, body ->
        String attributes = ''
        if (attrs['test']) {
            attrs.remove('test')
           if (attrs.keySet().contains("action") || attrs.keySet().contains("controller")) {
                out << g.link(attrs, body)
            } else {
                attributes = "${attrs.collect {k, v -> " $k=\"$v\"" }.join('')}>"
                out << ""
            }

        } else {
            out << body()
        }
    }
}

Now since the Tag is created condition test and and link can be combined into one


text                                        

or

 
     Text 

_____________________
Hitesh Bhatia
hitesh@intelligrape.com
_____________________

FOUND THIS USEFUL? SHARE IT

comments (1 “Test Link”)

  1. Bhagwat Kumar

    Nice blog. Specially for the statement :

    attributes = “${attrs.collect {k, v -> ” $k=”$v”” }.join(”)}>”

    This statement is excellent which outputs all the attributes in a string irrespective of the no. of attributes passed with the tag.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *