简体   繁体   English

如何手动渲染缩放模板?

[英]How to render a scalate template manually?

I want to try Scalate in this way:我想以这种方式尝试Scalate

  1. Provide a scalate template, say: index.html提供一个 scalate 模板,比如:index.html
  2. Use scala code to render it with some data manually使用scala代码手动渲染一些数据
  3. Any template format is OK(mustache, Scaml, SSP, Jade)任何模板格式都可以(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 :为了让这个问题更清楚,所以我有这样一个模板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") .我想用用户实例User(name="Mike")渲染它。 How to do it?怎么做?

Suppose you have the following simple_example.mustache template:假设您有以下simple_example.mustache模板:

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

Once you get past the initial learning hump, Scalate is actually pretty nice to work with (the docs don't make the lib easy to use).一旦你克服了最初的学习障碍,Scalate 实际上非常好用(文档并没有让 lib 易于使用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM