我有一个
spring-boot
申请。我使用 Mustache
作为模板引擎来显示 html 页面。我正在尝试找到一种使用 Mustache 作为模板引擎的方法,但我不想将其显示为 html 响应,而是希望将其显示为 string
,以便我可以在其他一些功能中使用它。可以吗?
试试这个,它对我有用
public String render(String text, JSONObject substitutionData, boolean html) throws IOException {
MustacheFactory mustacheFactory = new DefaultMustacheFactory();
Mustache mustache = mustacheFactory.compile(new StringReader(text), "text");
StringWriter stringWriter = new StringWriter();
mustache.execute(stringWriter, substitutionData.toMap()).flush();
return stringWriter.toString();
}