5

I want to try Scalate in this way:

  1. Provide a scalate template, say: index.html
  2. Use scala code to render it with some data manually
  3. Any template format is OK(mustache, Scaml, SSP, Jade)

But I sad found nothing to do this even if I have read all the documentation and source code I found.

In order to make this question more clear, so I have such a template user.html:

<%@ var user: User %>
<p>Hi ${user.name},</p>
#for (i <- 1 to 3)
<p>${i}</p>
#end
<p>See, I can count!</p>

I want to render it with a user instance User(name="Mike"). How to do it?

4

1 回答 1

1

假设您有以下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 实际上非常适合使用(文档并没有使 lib 易于使用)。

于 2020-12-22T02:56:19.513 回答