Skip to content

Instantly share code, notes, and snippets.

@nise-nabe
Created February 24, 2013 17:52
Show Gist options
  • Save nise-nabe/5024801 to your computer and use it in GitHub Desktop.
Save nise-nabe/5024801 to your computer and use it in GitHub Desktop.
package controllers
import play.api._
import http.{Writeable, ContentTypeOf, ContentTypes}
import mvc.Codec
import play.api.Play.current
import org.fusesource.scalate.layout.DefaultLayoutStrategy
import collection.JavaConversions._
object Scalate {
import org.fusesource.scalate._
import org.fusesource.scalate.util._
var format = Play.configuration.getString("scalate.format") match {
case Some(configuredFormat) => configuredFormat
case _ => "scaml"
}
lazy val scalateEngine = {
val engine = new TemplateEngine
engine.resourceLoader = new FileResourceLoader(Some(Play.getFile("app/views")))
engine.layoutStrategy = new DefaultLayoutStrategy(engine, "app/views/layouts/default." + format)
engine.classpath = "tmp/classes"
engine.workingDirectory = Play.getFile("tmp")
engine.combinedClassPath = true
engine.classLoader = Play.classloader
engine
}
def apply(template: String) = Template(template)
case class Template(name: String) {
def render(args: java.util.Map[String, Any]) = {
ScalateContent{
scalateEngine.layout(name, args.map {
case (k, v) => k -> v
} toMap)
}
}
}
case class ScalateContent(val cont: String)
implicit def writeableOf_ScalateContent(implicit codec: Codec): Writeable[ScalateContent] = {
Writeable[ScalateContent]((scalate:ScalateContent) => codec.encode(scalate.cont))
}
implicit def contentTypeOf_ScalateContent(implicit codec: Codec): ContentTypeOf[ScalateContent] = {
ContentTypeOf[ScalateContent](Some(ContentTypes.HTML))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment