grails - 有没有一种有效的方法可以从从数据库加载的字符串中调用 Grails 模板?

标签 grails sitemesh

我将用户可编辑的文章存储在数据库中。用户可以在文章中插入一些简单的小部件(图表等)。到目前为止,我已经通过让用户插入像 [graph-1] 这样的图形而不是进行字符串搜索和替换来实现这一点作为概念证明。

我想知道是否有更有效的方法从字符串调用模板?也许涉及 Sitemesh?

最佳答案

这是一个代码示例,它采用模板和带有变量的绑定(bind) Map 并将其呈现为字符串:

import groovy.text.SimpleTemplateEngine

def engine = new SimpleTemplateEngine()
String templateContent = 'hello ${name}'
def binding = [name: 'world']

String rendered = engine.createTemplate(templateContent).make(binding).toString()

您只需将硬编码的“templateContent”替换为数据库中的字符串,并使用对该模板有意义的任何数据填充绑定(bind)映射。

关于grails - 有没有一种有效的方法可以从从数据库加载的字符串中调用 Grails 模板?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2573753/

相关文章:

grails - 参数#1未设置错误

hibernate - Hibernate HQL选择不同的

validation - 如何使用 @Validateable 和 @BindUsing 对 Grails 命令对象进行单元测试

grails - Grails应用程序的种子数据

java - sitemesh代替全 View spring

spring-mvc - Sitemesh 不修饰返回的 View

java - 为什么在装饰器中调用安全认证属性 `principal.displayName`会抛出异常?

java - Sitemesh 3 中的多个正文标签

html - eot 字体在我的女孩申请中没有受到影响