Skip to content

Instantly share code, notes, and snippets.

@afternoon
Created October 1, 2012 16:14
Show Gist options
  • Save afternoon/3812769 to your computer and use it in GitHub Desktop.
Save afternoon/3812769 to your computer and use it in GitHub Desktop.
Use Scalate in a new Play 2.0 project
From 5942a6b9041cce90c326664f206592782157353f Mon Sep 17 00:00:00 2001
From: Ben Godfrey <bpgodfrey@gmail.com>
Date: Mon, 1 Oct 2012 17:18:31 +0100
Subject: [PATCH] Use Scalate to render templates
---
app/controllers/Application.scala | 4 +--
app/lib/ScalateIntegration.scala | 53 +++++++++++++++++++++++++++++++++++++++
app/views/index.jade | 3 +++
app/views/index.scala.html | 7 ------
app/views/layouts/default.jade | 9 +++++++
app/views/main.scala.html | 15 -----------
conf/application.conf | 2 ++
project/Build.scala | 1 +
8 files changed, 70 insertions(+), 24 deletions(-)
create mode 100644 app/lib/ScalateIntegration.scala
create mode 100644 app/views/index.jade
delete mode 100644 app/views/index.scala.html
create mode 100644 app/views/layouts/default.jade
delete mode 100644 app/views/main.scala.html
diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala
index acafe6a..096753c 100644
--- a/app/controllers/Application.scala
+++ b/app/controllers/Application.scala
@@ -6,7 +6,7 @@ import play.api.mvc._
object Application extends Controller {
def index = Action {
- Ok(views.html.index("Your new application is ready."))
+ Ok(Scalate("index.jade").render('title -> "Hello world!"))
}
-}
\ No newline at end of file
+}
diff --git a/app/lib/ScalateIntegration.scala b/app/lib/ScalateIntegration.scala
new file mode 100644
index 0000000..f7d71f3
--- /dev/null
+++ b/app/lib/ScalateIntegration.scala
@@ -0,0 +1,53 @@
+package controllers
+
+import play.api._
+import http.{Writeable, ContentTypeOf, ContentTypes}
+import mvc.Codec
+import play.api.Play.current
+import org.fusesource.scalate.layout.DefaultLayoutStrategy
+
+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: (Symbol, Any)*) = {
+ ScalateContent{
+ scalateEngine.layout(name, args.map {
+ case (k, v) => k.name -> v
+ } toMap)
+ }
+ }
+
+ }
+
+ case class ScalateContent(val cont: String)
+
+ implicit def writeableOf_ScalateContent(implicit codec: Codec): Writeable[ScalateContent] = {
+ Writeable[ScalateContent](scalate => codec.encode(scalate.cont))
+ }
+
+ implicit def contentTypeOf_ScalateContent(implicit codec: Codec): ContentTypeOf[ScalateContent] = {
+ ContentTypeOf[ScalateContent](Some(ContentTypes.HTML))
+ }
+}
\ No newline at end of file
diff --git a/app/views/index.jade b/app/views/index.jade
new file mode 100644
index 0000000..949249f
--- /dev/null
+++ b/app/views/index.jade
@@ -0,0 +1,3 @@
+-@ var title: String = "Page"
+
+h1= title
diff --git a/app/views/index.scala.html b/app/views/index.scala.html
deleted file mode 100644
index 9f460d3..0000000
--- a/app/views/index.scala.html
+++ /dev/null
@@ -1,7 +0,0 @@
-@(message: String)
-
-@main("Welcome to Play 2.0") {
-
- @play20.welcome(message)
-
-}
\ No newline at end of file
diff --git a/app/views/layouts/default.jade b/app/views/layouts/default.jade
new file mode 100644
index 0000000..e7bd066
--- /dev/null
+++ b/app/views/layouts/default.jade
@@ -0,0 +1,9 @@
+-@ var body: String
+-@ var title: String = "Page"
+
+!!!5
+html
+ head
+ title= title
+ body
+ != body
diff --git a/app/views/main.scala.html b/app/views/main.scala.html
deleted file mode 100644
index d17217f..0000000
--- a/app/views/main.scala.html
+++ /dev/null
@@ -1,15 +0,0 @@
-@(title: String)(content: Html)
-
-<!DOCTYPE html>
-
-<html>
- <head>
- <title>@title</title>
- <link rel="stylesheet" media="screen" href="@routes.Assets.at("stylesheets/main.css")">
- <link rel="shortcut icon" type="image/png" href="@routes.Assets.at("images/favicon.png")">
- <script src="@routes.Assets.at("javascripts/jquery-1.7.1.min.js")" type="text/javascript"></script>
- </head>
- <body>
- @content
- </body>
-</html>
diff --git a/conf/application.conf b/conf/application.conf
index b4141c6..ebd3f2a 100644
--- a/conf/application.conf
+++ b/conf/application.conf
@@ -45,3 +45,5 @@ logger.play=INFO
# Logger provided to your application:
logger.application=DEBUG
+# Default Scalate template format (mustache, scaml, jade, ssp)
+scalate.format=jade
diff --git a/project/Build.scala b/project/Build.scala
index 5f537af..a35af9d 100644
--- a/project/Build.scala
+++ b/project/Build.scala
@@ -9,6 +9,7 @@ object ApplicationBuild extends Build {
val appDependencies = Seq(
// Add your project dependencies here,
+ "org.fusesource.scalate" % "scalate-core" % "1.5.3"
)
val main = PlayProject(appName, appVersion, appDependencies, mainLang = SCALA).settings(
--
1.7.12.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment