2016-08-23 62 views
0

如何在Grails中最好地实现类似短代码(即它在Wordpress中称为的内容)。如何在Grails中最好地实现类似短代码的东西

我有一个Grails应用程序,用户可以在其中输入文本。该文本保存在数据库中。文本应该包含像“短代码”:

class Page(){ 

String text = "please display [form A] above here." 

} 

在我看来,我显示我的域对象的值文本。

${domainObject.text} 

例如:“[form A]”应该在放置文本的位置显示引用的表单A.

在Grails中我可以做到这一点的最佳方式是什么。

+1

看起来你需要提供一个你想要的更好的例子。我能够理解的是,用户输入点击formA。然后formA应该成为一个链接。 –

+0

试图让它更清楚。 – user3675091

回答

0

最好的办法是使用SimpleTemplateEngine。使用这个你可以有标准的${}块来指定变量。

使用方法如下图所示:

String renderTemplate(Reader template, Map bindings) { 
     try { 
      def engine = new groovy.text.SimpleTemplateEngine() 
      return engine.createTemplate(template).make(bindings) 
     } catch (MissingPropertyException mpe) { 
      log.error("Missing property in template binding", mpe) 
     } 
     return "" 
    } 

例子:

String text = "please display ${formA} above here." 

String parsedText = renderTemplate(new StringReader(text),[formA:'http://www.yourlink.com']) 

希望它可以帮助!

相关问题