Tuesday, June 22, 2010

Grails pageScope variable in GSPs and TagLibraries


Grails pageScope variable is available to you in Tag Libraries and GSPs and contains a wealth of state information. For instance, you can set an attribute in the model in a controller and then reference it in a TagLibrary through the pageScope variable without having to pass it through the tag attributes

Controller:

def myAction = {
    //...
    boolean readOnly = true
    render(view: 'edit', model: [bookInstance:book, readOnly:readOnly])
}

GSP:

    <g:myInputFieldTag name="title" value="${bookInstance.title}"/>

TagLibrary

def myInputFieldTag = { attrs->
    if (pageScope.readOnly){
        out << attrs.value
    } else {
        out << //...
    }
}

You can also use it within a GSP or to get access to variables you have set in a GSP or to store variables in page scope in a tag library.
Docs:
http://grails.org/doc/latest/ref/Tag%20Libraries/pageScope.html
and
http://grails.org/1.0-RC1+Release+Notes#Improved%20support%20for%20scopes

No comments:

Post a Comment