Call Grails 2 r:require module from a taglib

145 Views Asked by At

Having a taglib that uses an external js library to show user notification. Since calling this taglib in many views, I'm wondering how to require the library js module directly from the tag, without doing it from each gsp view.

Currently I'm using this code in the gsp file:

<g:require modules="pnotify"/>

And below in the taglib source code:

class MyTagLib {

    static namespace = "my"

    def myHtmlWithNotifications = { attrs ->
        out << "<table>"
        // ...
        out << "</table>"
    }
}
1

There are 1 best solutions below

0
lifeisfoo On

Just call the require module from the taglib in this way:

class MyTagLib {

    static namespace = "my"

    def myHtmlWithNotifications = { attrs ->
        out << r.require([modules: "pnotify"])
        out << "<table>"
        // ...
        out << "</table>"
    }
}