templates - grails:如何将数据绑定(bind)到 GroovyPagesTemplateEngine 实例?

标签 templates grails

我尝试将数据绑定(bind)到使用 GroovyPagesTemplateEngine 创建的模板,但不能。
这是我能做到的。有人能帮忙吗?谢谢!

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))

我尝试了 template.make(data),但不起作用.....

最佳答案

试试这个:

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()

关于templates - grails:如何将数据绑定(bind)到 GroovyPagesTemplateEngine 实例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3907989/

相关文章:

c++ - 默认模板模板参数的语法

grails - Grails 如何使 `params` 可用于单例 Controller ? (Grails 内部结构)

grails - 处理snmp的软件

grails - 为什么Grails的单元,集成和功能测试阶段没有自己的环境?

grails - 在特定DIV中渲染TAGLIB

c++ - 在 C++ 中扩展模板参数的名称

c++ - 将引用显式传递到不期望引用的函数模板中会导致问题吗?

grails - 在 Grails/Groovy 中加载插件管理器时出错

java - 访问可以是 ConcurrentHashMap 的对象的 ConcurrentHashMap

html - 如何在 HTML/CSS 中实现 "blog"页面?