Skip to content

Instantly share code, notes, and snippets.

@geetchandratre
Created June 4, 2013 12:24
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geetchandratre/5705523 to your computer and use it in GitHub Desktop.
Save geetchandratre/5705523 to your computer and use it in GitHub Desktop.
Play 2.x + SBT + Scala + Jade
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"))
}
}
@lowi
Copy link

lowi commented Aug 14, 2014

For play framework 2.3, replace:
import play.api.templates.Html =>
import play.twirl.api.Html
import play.twirl.api.HtmlFormat

new Html(new StringBuilder(jadeConfig.renderTemplate(jadeTemplate, context))) =>
Html.apply(new StringBuilder(jadeConfig.renderTemplate(jadeTemplate, context)).toString())

return new Html(new StringBuilder) =>
Html.apply("")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment