Is there a way to call a taglib closure from inside the grails console? I want to be able to get at the message tag within the grails console and I can not figure this out...
You can get the configured taglib, but most expect to be running in the context of a web request. To get around that you can bind a mock request:
import grails.util.GrailsWebUtil
GrailsWebUtil.bindMockWebRequest ctx
def g = ctx.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib')
String message = g.message(code: 'default.button.delete.confirm.message')
You can also get messages for other languages by setting the locale of the request, e.g.
import grails.util.GrailsWebUtil
def webRequest = GrailsWebUtil.bindMockWebRequest(ctx)
webRequest.currentRequest.addPreferredLocale(Locale.GERMANY)
def g = ctx.getBean('org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib')
String message = g.message(code: 'default.button.delete.confirm.message')
Using @Burt console plugin this is even easier as we don't have to mock the web request...
import org.codehaus.groovy.grails.plugins.web.taglib.ValidationTagLib
// Getting the class name to reduce horizontal
// scrolling in StackOverflow
def g = ctx.getBean(ValidationTagLib.class.getName())
g.message(code: 'default.button.delete.confirm.message');
You can get a list of all the tagLibs in your application by running this code in the console...
// prints a bean name per line.
ctx.getBeanNamesForType(Object).findAll {
it =~ /.*TagLib$/
} .sort() {println it}
// add false to prevent console printing the map out
false
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With