Dónal
Dónal

Reputation: 187529

grails access controller from taglib

Is it possible to access the current controller instance from within a TagLib? For example:

class FooTagLib {
    static namespace = 'foo'

    def msg = { attrs, body ->          
      // Can I get a reference to the current controller here?
    }
}

I want to do this because I store some data in a property of the controller and want to access it within the TagLib. I realise this may sound strange, but just humour me....

Upvotes: 2

Views: 2192

Answers (2)

fabien7474
fabien7474

Reputation: 16562

Inside your msg tagLib:

grailsApplication.getArtefactByLogicalPropertyName('Controller', pageScope.controllerName)

Like Views, you have access to the current controller and action through controllerName and actionName

Upvotes: 5

Gregg
Gregg

Reputation: 35864

Try something like this...

def ctl = grailsApplication.getArtefactByLogicalPropertyName('Controller', 'whateverController') 

Upvotes: 0

Related Questions