tags:

views:

111

answers:

1

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
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
if the template's code is in a String, consider using the SimpleTemplateEngine http://groovy.codehaus.org/api/groovy/text/SimpleTemplateEngine.html
Stefan