Rendering GSP as Strings in Grails
In grails if you want to store GSP pages and render them as strings, you can! but its a little tricksy.
The 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 response then it is much simpler but if you want to render the contents to a string, it gets a little weirder. Just spent the better half of 2 hours trying to figure this one out with (alot of) help from the bobbytek and mikenicholson over at #grails on freenode. It goes like this:
import org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequest
import org.springframework.web.context.request.RequestContextHolder
import groovy.text.Template
class WorkshopsController {
GroovyPagesTemplateEngine groovyPagesTemplateEngine
private String renderTemplate(String content, Map model)
{
// create a template using the content, "somepage" is arbitrary
def template = groovyPagesTemplateEngine.createTemplate(content, "somepage.gsp")
// Get the current request and create a new output stream
def requestAttributes = RequestContextHolder.getRequestAttributes()
def strWriter = new StringWriter()
// Replace the requests output stream with the new one, remember the old one!
def originalOut = requestAttributes.getOut()
requestAttributes.setOut(strWriter)
// Render that bad boy, note the "writeTo" basically ignores the stream you give it
// It renders straight to the response, thats what that stuff before was for
try {
def renderedTemplate = template.make(model)
renderedTemplate.writeTo(strWriter)
}
catch(Exception e) {}
finally {requestAttributes.setOut(originalOut)}
return strWriter.toString()
}
def index = {
def myStr = renderTemplate( " asdasdas ", [:])
// < myStr now == <a href="/file/download"> asdasdas </a>
render myStr // Works as expected
}
}
The comments and the links i’ve pasted above should get you most of the way to understanding whats going on here. The problem boils down to the writeTo function in the renderedTemplate. It seems to write directly to the request’s output stream. This means that if we want to see the results before they are rendered in a string we have to be a little sneaky and switch the output stream temporarily with our own.
Horrible… Just horrible
But it works!
enjoy

Great walk-through to a working readline, thanks!
thanks for the write up…
i have my CTL+A working again
Nice fix. Thanks. This was causing me all sorts of grief.
It worked, thank you. Note that snow leopard doesn’t come with wget installed by default, but one can just download the readline tar manually. In the build.sh file, the < signs don’t show up properly in your post, breaking copy-paste. To make this work I also had to delete a previously installed copy of pyreadline (just in case someone has the same problem).
@Maxime yes or you can use curl, whatever is easiest. Ah yes just sorted the ‘< sign’ in the build.sh bit. I didn’t have an old copy of pyreadline in my copy of python 2.6 which i also upgraded to after upgrading to snow leopard, but yes that may need to be removed.
Thanks for the corrections
Thanks for the fix. You no longer need to install readline manually, easy_install installs readline 2.6.4 which works beautifully.
Use pip, quit using easy_install
pip install readline
oh ok! didn’t know about pip, just installed it. It promises to be better than easy_install… sounds good to me