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:\\myToo开发者_StackOverflowls\\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.....
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()
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.
精彩评论