Skip to content

Instantly share code, notes, and snippets.

@dozed
Created July 6, 2015 18:12
Show Gist options
  • Save dozed/bfb35abd22e451434cd6 to your computer and use it in GitHub Desktop.
Save dozed/bfb35abd22e451434cd6 to your computer and use it in GitHub Desktop.
package foo
import java.io.{PrintWriter, StringWriter}
import org.fusesource.scalate.{TemplateEngine, DefaultRenderContext}
class SimpleTemplateEngine(templateSource: String) {
val templateEngine = new TemplateEngine()
def layoutTemplate(path: String, attributes: (String, Any)*): String = {
val uri = f"$templateSource/$path"
val buffer = new StringWriter()
val out = new PrintWriter(buffer)
val context = new DefaultRenderContext("", templateEngine, out)
attributes foreach {
case (k, v) => context.attributes(k) = v
}
templateEngine.layout(uri, context)
buffer.toString
}
}
package tests
import com.typesafe.config.ConfigFactory
import org.specs2.mutable._
import scala.concurrent.Await
class TemplateSpecs extends Specification {
val templateEngine = new SimpleTemplateEngine("/home/user/code/path/to/templates")
"Template engine" should {
"Render a jade template" in {
val text = templateEngine.layoutTemplate("mail.verify-account-html.jade", attrs1: _*)
text must not empty
}
"Render a mustache template" in {
val text = templateEngine.layoutTemplate("mail.verify-account-text.mustache", attrs1: _*)
text must not empty
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment