Azder
Azder

Reputation: 4728

Can I use grails tag outside of GSP?

For example, i can put

 <g:createLink controller="user" action="show" /> 

inside a .gsp file and it will work nicely.

But also I'd like to use the same closure createLink inside a .groovy file which is not part of the grails views

Upvotes: 15

Views: 10337

Answers (4)

Peter
Peter

Reputation: 29867

The native way to do this as of Grails 2.0 outside of controllers (so for services, async jobs, etc) is to use the LinkGenerator class. Works everywhere and mentioned in the official docs. See example here

http://mrhaki.blogspot.ca/2012/01/grails-goodness-generate-links-outside.html

Upvotes: 6

James Allman
James Allman

Reputation: 41188

For unmanaged classes you can reference the g taglib with:

def g = ApplicationHolder.application.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')

Upvotes: 7

Matt Christianson
Matt Christianson

Reputation: 149

Inject the grailsApplication into your service/filter.

def grailsApplication

And get the Spring bean.

def g = grailsApplication.mainContext.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ApplicationTagLib')
def userShow = g.createLink(controller: 'user', action: 'show')

Upvotes: 14

John Wagenleitner
John Wagenleitner

Reputation: 11035

You can use taglib methods from Grails controllers, for example:

def userShow = g.createLink(controller:"user", action:"show")

For builtin taglibs (or those in the g namespace) you can omit the namespace prefix in the method call.

Upvotes: 20

Related Questions