1

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

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

4

1 回答 1

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

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

于 2010-04-04T06:45:10.170 回答