Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a scalate template manually?

Tags:

scala

scalate

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?

like image 596
Freewind Avatar asked Nov 23 '22 13:11

Freewind


1 Answers

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

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).

like image 172
Powers Avatar answered Nov 25 '22 03:11

Powers