开发者

grails: how to bind data to GroovyPagesTemplateEngine instance?

开发者 https://www.devze.com 2023-01-19 08:01 出处:网络
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!

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号