2016-08-23 63 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']) 

希望它可以幫助!

相關問題