public
Created

Play 2.x + SBT + Scala + Jade

  • Download Gist
play_scala_jade
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
All you need to do is,
 
In your SBT Dependencies add,
 
"de.neuland" % "jade4j" % "0.3.11" from "https://raw.github.com/neuland/jade4j/master/releases/de/neuland/jade4j/0.3.11/jade4j-0.3.11.jar",
"com.googlecode.concurrentlinkedhashmap" % "concurrentlinkedhashmap-lru" % "1.3.1",
"org.apache.commons" % "commons-jexl" % "2.1.1"
 
You need apache libs as Jade4J uses them internally,
 
Create a file called JadeController.scala
 
import de.neuland.jade4j.Jade4J
import de.neuland.jade4j.JadeConfiguration
import de.neuland.jade4j.template.FileTemplateLoader
import de.neuland.jade4j.template.JadeTemplate
import play.api.mvc.Controller
import play.api.templates.Html
import scala.collection.JavaConversions._
import java.io.IOException
 
 
trait JadeController extends Controller {
val jadeConfig = new JadeConfiguration
 
val templateLoader = {
jadeConfig.setTemplateLoader(new FileTemplateLoader("app/views/", "UTF-8"))
jadeConfig.setMode(Jade4J.Mode.HTML)
jadeConfig.setPrettyPrint(true)
}
 
def render(template: String): Html = {
render(template, Map[String, String]())
}
 
def render(template: String, context: Map[String, Object]): Html = {
try {
val jadeTemplate: JadeTemplate = jadeConfig.getTemplate(template)
new Html(new StringBuilder(jadeConfig.renderTemplate(jadeTemplate, context)))
}
catch {
case ex: IOException => {
println("Missing file exception")
return new Html(new StringBuilder)
}
}
}
}
 
This will take care of the rendering,
 
and then in your controller,
 
object Application extends JadeController {
 
def index = Action {
Ok(render("index.jade"))
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.