4

是否可以使用 Pebble 模板引擎从字符串构建模板,而不必提供文件名?

val engine = PebbleEngine.Builder().build()
val writer = StringWriter();
engine.getTemplate("test.html").evaluate(writer);

test.html例如,我将如何提供以下格式的模板,而不是提供?

val template = "Hello {{world}} - {{count}} - {{tf}}"

我目前在 Pebble 2.2.1

<!-- Pebble -->
<dependency>
    <groupId>com.mitchellbosecke</groupId>
    <artifactId>pebble</artifactId>
    <version>2.2.1</version>
</dependency>

基于我收到的答案的解决方案:

val context = HashMap<String, Any>()
... 
val engine = PebbleEngine.Builder().loader(StringLoader()).build();
val writer = StringWriter();
engine.getTemplate(template).evaluate(writer, context);
println(writer.toString());
4

2 回答 2

5

根据测试,您只需要使用以下命令设置引擎StringLoader

val engine = PebbleEngine.Builder().loader(StringLoader()).build()
于 2016-05-26T10:07:37.943 回答
4

您需要StringLoader像这样向引擎提供 a:

val engine = PebbleEngine.Builder()
    .loader(StringLoader())
    .build()

val writer = StringWriter()
engine.getTemplate("<p>{{name}}</p>").evaluate(writer, mapOf("name" to "Stack Overflow"))
val result = writer.toString() // "<p>Stack Overflow</p>
于 2016-05-26T10:10:11.650 回答