开发者

Is there an efficient way to call a Grails template from a string loaded from db?

I store user editable articles in a database. Users can开发者_如何转开发 insert some simple widgets into the articles (graphs and so on). So far I've implemented this as a proof of concept by letting the user insert graphs like [graph-1] and than do a string search and replace.

I was wondering whether there are more efficient ways of calling templates from a string? Maybe involving Sitemesh?


Here's a sample of code that takes a template and a binding Map with variables and renders it to a string:

import groovy.text.SimpleTemplateEngine

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

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

You would just need to replace the hard-coded 'templateContent' with a string from the database and populate the binding map with whatever data makes sense for that template.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜