SpringBoot - Mustache - 以字符串形式获取模板

问题描述 投票:0回答:1

我有一个

spring-boot
申请。我使用
Mustache
作为模板引擎来显示 html 页面。我正在尝试找到一种使用 Mustache 作为模板引擎的方法,但我不想将其显示为 html 响应,而是希望将其显示为
string
,以便我可以在其他一些功能中使用它。可以吗?

spring-boot mustache
1个回答
0
投票

试试这个,它对我有用

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

}
© www.soinside.com 2019 - 2024. All rights reserved.