Mustache Scalate 与 Mustache Java

发布于 2024-12-27 14:52:57 字数 95 浏览 5 评论 0原文

我需要为我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

你在我安 2025-01-03 14:52:57

我刚刚经历了同样的过程(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.

檐上三寸雪 2025-01-03 14:52:57

我使用 Mustache 作为 scalatra-scalate 的一部分。这对我来说是唯一明智的选择,因为我已经投资了 Scalatra。如果有选择的话,我会彻底尝试 Mustache-java 。 Scalate 引擎(仍然?)有些古怪和不成熟。

我遇到的几个例子:

  • 如果您修改默认分隔符 {{=<% %>=}} 您将必须从那时起在分隔符周围添加空格,否则
  • 如果您想插入自定义处理, 解析器会阻塞缺少按键,有时你会被卡住
  • ,渲染会一次挂起几分钟,没有明显的原因(这是 Jade,而不是 Mustache,但无论如何它都相当可怕)

如果你不做复杂的事情,那么 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 modify the default delimiters {{=<% %>=}} you will have to add spaces around your delimiters from then on, otherwise the parser chokes
  • if you want to plug custom handling for missing keys, you're stuck
  • sometimes the rendering hangs for minutes at a time for no obvious reason (this was with Jade, not Mustache, but it's rather scary anyway)

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.

合约呢 2025-01-03 14:52:57

我刚刚经历了这个考验,因为我使用 Scala,所以我想使用 scalate,但我最终使用了 Mustache-Java 有两个原因:

  1. 没有简单的方法可以从类路径中的 /resources 目录(甚至只是普通的 Java 流对象)加载模板。这看起来非常基本,我很困惑为什么它不可用 OOB。
  2. 与 #1 类似,没有简单的方法可以从简单的 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 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:

  1. There is no trivial way to load template from /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.
  2. Similar to #1, there is no trivial way to load template from a simple 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):

"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 work:

"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"))
}

Finally, regarding lists: case classes are supported but for any list fields, they have to be the Java types. The following variations should work:

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", "
quot;, "30")).asJava)
  val sw = new StringWriter()
  mustache.execute(sw, plans)

  val list = Seq(Plan("essential", "
quot;, "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", "
quot;, "30")).asJava 

  // do not forget .asJava here
  mustache.execute(sw, Map("plans" -> list).asJava) 

  assert(sw.toString.contains("Name"))
}
魂牵梦绕锁你心扉 2025-01-03 14:52:57

Mustache-Java 的一个好处是 Java 相对于 Scala 编译速度非常快。

A benefit of Mustache-Java is that Java compiles very quickly relative to Scala.

岁月流歌 2025-01-03 14:52:57

Scala 可以轻松渲染 Mustache 模板,我宁愿使用相对直观的 Scala 库,而不是 Java 库。

假设您有以下 simple_example.mustache 模板:

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

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:

I like {{programming_language}}
The code is {{code_description}}

You can render the template with this code:

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)

Here's the result:

I like Scala
The code is pretty
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文