在 Pebble 模板引擎中解析字符串模板而不是文件

Parse String template instead of file in Pebble Template Engine

是否可以使用 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());

根据 to the tests,您只需使用 StringLoader:

设置引擎
val engine = PebbleEngine.Builder().loader(StringLoader()).build()

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

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>