Grails : Writing a taglib which uses a template to render data and keep it controller agnostic

grails, taglib, templates, view

Solution

Put template into `views/taglibTemplates/` and try:

out << render(template:"/taglibTemplates/iconText", model:modelMap)

or into `views/A/', and

out << render(template:"/A/iconText", model:modelMap)

Problem

I wrote a taglib which executes some logic and renders some data weaved into HTML. When I use the taglib in a view, the taglib expects to find the template in a relative subfolder of the view. Is there a way in which I can make the taglib pick up the template from a common folder like the layouts/ folder in view. This is how the taglib code looks: ``` class IconifiedTextTagLib { def renderIconText = { attrs, body -> //some processing logic to generate the modelMap out << render(template:"taglibTemplates/iconText", model:modelMap) } } ``` When I use the `<g:renderIconText />` tag in say a controller named A, then it expects the taglibTemplates/iconText to be present in the views/A/ folder. This is a problem because I need to be able to use it from multiple controllers. I need a way to put the templates in a folder like layouts/ so that it can be used in all the views. Any thoughts on how I can do this?

Original source