Reputation: 2662
I tried to bind data to a template created using GroovyPagesTemplateEngine, but cannot. Here is what I can as far I can go. Could some one help? Thanks!
import org.codehaus.groovy.grails.web.pages.GroovyPagesTemplateEngine
import org.springframework.core.io.FileSystemResource
File myfile = new File("c:\\myTools\\mydata.gsp")
def engine = new GroovyPagesTemplateEngine()
def data = ['data':'test']
def template = engine.createTemplate(new FileSystemResource(myfile))
I tried template.make(data), but does not work.....
Upvotes: 0
Views: 1610
Reputation: 5955
This should work for you:
def templateText = """
<h1>Hello $who</h1>
"""
def output = new StringWriter()
groovyPagesTemplateEngine.createTemplate(templateText, 'sample').make([who:'World']).writeTo(output)
render output.toString()
Just include groovyPagesTemplateEngine via dependency injection, the same way you would reference a service.
Upvotes: 0
Reputation: 75671
Try this:
import groovy.text.SimpleTemplateEngine
def engine = new SimpleTemplateEngine()
String templateContent = new File('c:/myTools/mydata.gsp').text
def binding = [data: 'test']
String rendered = engine.createTemplate(templateContent).make(binding).toString()
Upvotes: 1