我需要为我的Scala项目选择一个Mustache渲染引擎。似乎只有两个选择是Mustache-Java和Scalate?有什么比较吗?这两个中哪一个更稳定/性能更好?
发布于 2012-04-05 00:24:34
我刚刚经历了同样的过程(Mustache Scalate或Mustache Java)。我最终选择了Mustache Java,它工作得很好。
为什么是Mustache Java?因为我只想要胡须模板。Scalate不仅仅具有这种支持,而且我不想在我的代码库中添加更多的“东西”,而只是使用它的部分功能。
发布于 2012-01-26 03:34:53
我用八字胡作为scalatra scalate的一部分。这对我来说是唯一明智的选择,因为我已经投资了Scalatra。如果可以选择的话,我会彻底尝试一下mustache java。Scalate引擎(还在吗?)有点古怪和不成熟。
下面是我遇到的一些例子:
如果你不是在做复杂的事情,Scalate添加了一些漂亮的功能,比如默认模板之类的,这可能会对你有所帮助。
发布于 2021-03-13 23:42:08
我刚刚经历了这场磨难,因为我正在使用Scala,所以我非常想使用scalate,但我最终还是使用了Mustache-Java,原因有两个:
/resources目录(甚至是普通的Java流对象)中加载模板。这看起来非常基础,我很困惑为什么这个不能使用。String加载模板。如果你想这样做,你还需要编写一个定制的加载器。为什么?这是基于on their documentation的。如果你使用Scalate来生成服务器端网页,它看起来非常强大和灵活,但对于简单的用例,它会带来太多的负担。
我在Mustache-Java中发现的一件事是,你需要将数据转换成Java map,否则它就不能工作(至少在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"))
}Inverted sections工作:
"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"))
}最后,关于列表:支持case类,但对于任何列表字段,它们都必须是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"))
}https://stackoverflow.com/questions/8889300
复制相似问题