Suppose I have a gsp snippet stored in my database. How do I programatically merge it with a data model to produce a string.
+2
A:
The applicationContext of any Grails app contains a bean named
groovyPagesTemplateEngine
By default this is a instance of GroovyPagesTemplateEngine. So you might use code like this in your controller or service:
class MyService/MyController {
def groovyPagesTemplateEngine
String renderGSPToString(String uri, Map model) {
groovyPagesTemplateEngine.createTemplate(uri).make(model).toString()
}
}
NB: this snippet is not really taken from running code, it should just clarify the idea.
Stefan
2010-04-17 12:59:38
In fact I want the 'createTemplate' that takes the GSP text itself as input, instead of the uri of a file. But that's the way. Thanks.
tuler
2010-04-19 10:33:19
if the template's code is in a String, consider using the SimpleTemplateEngine http://groovy.codehaus.org/api/groovy/text/SimpleTemplateEngine.html
Stefan
2010-04-19 12:17:23