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?

Freewind
  • 193,756
  • 157
  • 432
  • 708
  • Scalate has brutal documentation: a bunch of semi-connected thoughts with no context or examples. My team had far greater productivity using the Twirl library a la carte, if you dont have time to parse the source code for scalate and figure everything out yourself. – Azeli Jul 11 '16 at 22:58
  • @Azeli, I totally agree with you, and also switched to Twirl in earlier time. Thank you – Freewind Jul 12 '16 at 09:22
  • 1
    It's been two and a half years and it still seems that way. – Karsten Jan 29 '19 at 13:35

1 Answers1

1

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

Machavity
  • 30,841
  • 27
  • 92
  • 100
Powers
  • 18,150
  • 10
  • 103
  • 108