Skip to content

@geetchandratre /play_scala_jade
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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

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
Something went wrong with that request. Please try again.