Mustache Scalate 与 Mustache Java
我需要为我的 Scala 项目选择一个 Mustache 渲染引擎。似乎只有 Mustache-Java 和 Scalate 两个选择?有比较吗?两者中哪一个更稳定/性能更好?
I need to pick a Mustache rendering engine for a Scala project of mine. Seems like the only two choices are Mustache-Java and Scalate? Are there any comparisons? Which one is the more stable/performant of the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我刚刚经历了同样的过程(Mustache Scalate 或 Mustache Java)。我最终选择了 Mustache Java,它工作得很好。
为什么选择 Mustache Java?因为我想要的只是小胡子模板。 Scala 不仅仅提供这种支持,而且我不想向我的代码库添加更多“东西”,而只想使用其部分功能。
I just went through this same process (Mustache Scalate or Mustache Java). I ended up going with Mustache Java, and it's working fine.
Why Mustache Java? Because all I wanted was Mustache templates. Scalate has more than just this support, and I didn't want to add more "stuff" to my code base but only use part of its functionality.
我使用 Mustache 作为 scalatra-scalate 的一部分。这对我来说是唯一明智的选择,因为我已经投资了 Scalatra。如果有选择的话,我会彻底尝试 Mustache-java 。 Scalate 引擎(仍然?)有些古怪和不成熟。
我遇到的几个例子:
如果你不做复杂的事情,那么 Mustache 就可以正常工作,并且Scalate 添加了一些漂亮的功能,例如默认模板等,可能会对您有所帮助。
I was using mustache as part of scalatra-scalate. This is the only sensible choice for me as I'm already invested in Scalatra. Given the choice, I'd try mustache-java thoroughly. The Scalate engines are (still?) somewhat quirky and immature.
A few examples I ran into:
If you're not doing complex things mustache works fine though, and Scalate adds some nifty features like default templates and such that might help you.
我刚刚经历了这个考验,因为我使用 Scala,所以我想使用 scalate,但我最终使用了 Mustache-Java 有两个原因:
/resources
目录(甚至只是普通的 Java 流对象)加载模板。这看起来非常基本,我很困惑为什么它不可用 OOB。String
加载模板。如果您想这样做,您还需要编写一个自定义加载程序。为什么?这是基于他们的文档。如果您使用 Scalate 生成服务器端网页,它看起来非常强大且灵活,但对于简单的用例,它会带来太多负担。
我在 Mustache-Java 中发现的一件事是,您需要将数据转换为 Java 映射,否则它不起作用(至少对于 Scala 2.12):
反向部分工作:
最后,关于列表:支持案例类,但对于任何列表字段,它们必须是 Java 类型。以下变体应该有效:
I just went through this ordeal and in as much as I wanted to use scalate since I'm using Scala, I ended up using Mustache-Java for two reasons:
/resources
dir in classpath (or even just the normal Java stream objects). This seems very basic and I'm baffled why this is not available OOB.String
. You also need to write a custom loader if you want to do it. Why?This is based on their documentation. Scalate looks very powerful and flexible if you're using it to generate server-side web pages, but for simple use cases, it incurs too much baggage.
One thing I discovered in Mustache-Java, is that you need to convert the data into Java map or it doesn't work (at least with Scala 2.12):
Inverted sections work:
Finally, regarding lists: case classes are supported but for any list fields, they have to be the Java types. The following variations should work:
Mustache-Java 的一个好处是 Java 相对于 Scala 编译速度非常快。
A benefit of Mustache-Java is that Java compiles very quickly relative to Scala.
Scala 可以轻松渲染 Mustache 模板,我宁愿使用相对直观的 Scala 库,而不是 Java 库。
假设您有以下
simple_example.mustache
模板:您可以使用以下代码呈现该模板:
结果如下:
Scalate makes it easy to render Mustache templates and I'd rather use a relatively intuitive Scala lib than a Java lib.
Suppose you have the following
simple_example.mustache
template:You can render the template with this code:
Here's the result: