有没有一种有效的方法从db加载的字符串中调用Grails模板?

Kim*_*ble 1 grails sitemesh

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

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

Bur*_*ith 5

下面是一个代码示例,它采用模板和带有变量的绑定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()
Run Code Online (Sandbox Code Playgroud)

您只需要用数据库中的字符串替换硬编码的"templateContent",并使用对该模板有意义的任何数据填充绑定映射.