Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling TagLib stuff in Grails console?

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...

like image 381
intargc Avatar asked May 06 '10 15:05

intargc


2 Answers

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')
like image 142
Burt Beckwith Avatar answered Sep 27 '22 20:09

Burt Beckwith


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 
like image 22
chim Avatar answered Sep 27 '22 21:09

chim