我需要为我的Scala项目选择一个胡子(Mustache)渲染引擎。看起来只有Mustache-Java和Scalate这两个选择?有没有比较资料?哪一个更稳定/性能更好?
我需要为我的Scala项目选择一个胡子(Mustache)渲染引擎。看起来只有Mustache-Java和Scalate这两个选择?有没有比较资料?哪一个更稳定/性能更好?
我刚刚经历了与您同样的过程(Mustache Scalate或Mustache Java)。最终,我选择了Mustache Java,并且它工作得很好。
为什么选择Mustache Java?因为我只需要Mustache模板。Scalate提供的支持更多,而我不想在我的代码库中添加更多的“东西”,只想使用其部分功能。
/resources
目录加载模板(甚至只是普通的Java流对象)。这似乎非常基本,我很困惑为什么没有开箱即用。String
中加载模板。如果您要这样做,您还需要编写自定义加载器。为什么?这基于他们的文档。如果您将其用于生成服务器端网页,则Scalate看起来非常强大和灵活,但对于简单的用例,它会产生太多负担。
在Mustache-Java中,我发现一件事情,那就是您需要将数据转换为Java映射,否则它不起作用(至少在Scala 2.12中):
"Mustache test" should "succeed" in {
import scala.collection.JavaConverters._
val template = "Hello, {{name}}"
val mf = new DefaultMustacheFactory()
val mustache = mf.compile(new StringReader(template), "sample")
val map = Map("name" -> "Joe")
val sw = new StringWriter()
mustache.execute(sw, map.asJava) // if you don't use .asJava here it's not going to work
assert(sw.toString.contains("Joe"))
}
反转部分 起作用:
"Mustache test exclusion" should "succeed" in {
import scala.collection.JavaConverters._
val template = "Hello, {{^exclude}}am I excluded?{{/exclude}}"
val mf = new DefaultMustacheFactory()
val mustache = mf.compile(new StringReader(template), "sample")
val map = Map("exclude" -> true)
val sw = new StringWriter()
mustache.execute(sw, map.asJava)
assert(!sw.toString.contains("excluded"))
}
最后,关于列表:支持案例类,但对于任何列表字段,它们必须是Java类型。以下变体应该可行:
case class Plan(name: String,
currency: String,
price: String)
"Mustache test with object context with list" should "succeed" in {
import scala.collection.JavaConverters._
import java.util.{List => JavaList}
val template =
"""{{#plans}}
|Name: {{name}}
|Price: {{currency}}{{price}}
|{{/plans}}""".stripMargin
val mf = new DefaultMustacheFactory()
val mustache = mf.compile(new StringReader(template), "sample")
// note the Java list type here
case class Plans(plans: JavaList[Plan])
val plans = Plans(Seq(Plan("essential", "$", "30")).asJava)
val sw = new StringWriter()
mustache.execute(sw, plans)
val list = Seq(Plan("essential", "$", "30")).asJava
mustache.execute(sw, Map("plans" -> list).asJava)
assert(sw.toString.contains("Name"))
}
"Mustache test with list" should "succeed" in {
import scala.collection.JavaConverters._
val template =
"""{{#plans}}
|Name: {{name}}
|Price: {{currency}}{{price}}
|{{/plans}}""".stripMargin
val mf = new DefaultMustacheFactory()
val mustache = mf.compile(new StringReader(template), "sample")
val sw = new StringWriter()
// do not forget .asJava here
val list = Seq(Plan("essential", "$", "30")).asJava
// do not forget .asJava here
mustache.execute(sw, Map("plans" -> list).asJava)
assert(sw.toString.contains("Name"))
}
I like {{programming_language}}
The code is {{code_description}}
您可以使用以下代码渲染模板:
import org.fusesource.scalate.TemplateEngine
val sourceDataPath = os.pwd/"simple_example.mustache".toString
val engine = new TemplateEngine
val someAttributes = Map(
"programming_language" -> "Scala",
"code_description" -> "pretty"
)
engine.layout(sourceDataPath, someAttributes)
这是结果:
I like Scala
The code is pretty