views:

34

answers:

0

Lets assume that I have the following configuration in my conf/InjectionConfig.groovy file:

x {
    a = { attrs, body -> out << "hello" }
    b = { attrs, body ->  out << "goodbye" }
}

and that I have a simple taglib such as

class XTagLib {
    static namespace = "x"
}

What I want to do is that when I type <x:a /> to any of my views, it would print hello. I've already tried to inject these to the metaclass of the taglib as both property and method but neither seem to work. As an example, here's basically what I'm doing right now in a service:

public void afterPropertiesSet() throws Exception {
    GroovyClassLoader classLoader = new GroovyClassLoader(getClass().classLoader)
    def slurper = new ConfigSlurper(GrailsUtil.environment)
    ConfigObject xConfig
    try {
        xConfig = slurper.parse(classLoader.loadClass('InjectionConfig'))
    }
    catch (e) {
        e.printStackTrace()
    }
    xConfig.x.each({
        if ( !XTagLib.metaClass.hasMetaProperty(it.key) ) {
            XTagLib.metaClass.registerBeanProperty(it.key, { args ->
                def attrs = args[0], body = args[1]
                it.value.call(attrs, body)
            }
        }
    })
}

Am I just doing it wrong or is this even possible currently?