Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 577
Ritesh M Nayak Avatar asked May 15 '12 07:05

Ritesh M Nayak


2 Answers

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)
like image 125
Igor Artamonov Avatar answered Sep 19 '22 23:09

Igor Artamonov


on grails 2.3.8 I had to use as such

make sure the template filename starts with underscore

_mytemplate.gsp

in your tag library class

out << g.render(template: '/taglibTemplates/mytemplate')
like image 44
elixir Avatar answered Sep 18 '22 23:09

elixir