Skip navigation

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).



  1. I’m not sure, but my guess would be that your first attempt didn’t work because the template engine you created lacked context (and any state Grails might have otherwise added). In contrast, when you declared a variable following the naming conventions, Grails automatically injected a template engine with the necessary context and state. You can do the same thing with services: (see the Dependency Injection section)

    By the way, someone named Dima started collecting information on and documenting how things work in Grails behind the scenes. If you’re interested, it’s available at

    • Thanks man. Yeah, that was definitely my problem with the first situation. However, I tried manually injecting the context and nothing seemed to work.

  2. The second approach usess magic Grails dependency injection.

One Trackback/Pingback

  1. […] process to do so does not seem to be very well documented, though it has been talked about recently and implemented in the mail plugin. If your goal is simply to render gsp directly to the […]

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: