Skip navigation

Monthly Archives: June 2009

I recently added functionality to the CMS I’ve been working on for my company.  It allows us to enter GSP tags into the content that is being posted to our Grails website.  These tags would then be properly rendered through Grails.

After searching around for hours, I thought I had found a solution to pass a string through the grails template engine:

new GroovyPagesTemplateEngine().createTemplate(attrs.templateString, “somepage.gsp”).make(attrs.model).writeTo(out)

This works great for our ${} style tags, however it does not work for anything in a tag library.  Again, I went to google and I could not find anything!  Finally, I found an obscure post describing how to build e-mails using GSP.  There isn’t any explanation as to how the template engine is acquired, but it seems to work.  In your tag lib, or controller create a member variable:

GroovyPagesTemplateEngine groovyPagesTemplateEngine

It seams as though you have to name it exactly as it is above.  You can then call the same method as before:

groovyPagesTemplateEngine.createTemplate(attrs.templateString, “somepage.gsp”).make(attrs.model).writeTo(out)

And bam, all of your tag libs are referenced properly!  Also, you can put whatever you want in “somepage.gsp”.  It doesn’t seem to matter.  However, I would assume for performance purposes, if the template is expected to be the same, make sure the page name is the same (so that grails can do its internal caching magic).

Advertisements