Skip to content

Commit bd3d71e

Browse files
author
Julien Bille
committed
upgrade mustache version and do some basic unit test
1 parent 79f0956 commit bd3d71e

File tree

8 files changed

+63
-29
lines changed

8 files changed

+63
-29
lines changed

project-code/app/org/jba/Mustache.scala

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package org.jba
22

3-
import com.github.mustachejava._
4-
import com.twitter.mustache._
3+
import com.github.mustachejava.DefaultMustacheFactory
4+
import com.twitter.mustache.ScalaObjectHandler
55
import java.io.{StringWriter, InputStreamReader}
66

77
import org.apache.commons.lang.StringEscapeUtils
@@ -16,14 +16,11 @@ class MustachePlugin(app: Application) extends Plugin {
1616

1717
lazy val instance = {
1818
val i = new JavaMustache
19-
i.checkFiles
20-
//i.loadAllTemplate
2119
i
2220
}
2321

2422
override def onStart(){
2523
Logger("mustache").info("start on mode: " + app.mode)
26-
instance.checkFiles
2724
instance
2825
}
2926

@@ -53,25 +50,20 @@ class JavaMustache extends MustacheAPI{
5350
Logger("mustache").debug("read in factory: " + rootPath + resourceName + ".html")
5451
val input = Play.current.resourceAsStream(rootPath + resourceName + ".html").getOrElse(throw new Exception("mustache: could not find template: " + resourceName))
5552
new InputStreamReader(input)
56-
}
53+
}
5754
}
58-
factory.setObjectHandler(new TwitterObjectHandler)
55+
factory.setObjectHandler(new ScalaObjectHandler)
5956
factory
6057
}
6158

62-
private[jba] def checkFiles {
63-
if(!Play.getFile("app" + fs + "assets").exists())
64-
Logger.warn("app" + fs + "assets" + fs + "mustache directory is needed for mustache plugin")
65-
}
66-
6759
private def readTemplate(template: String) = {
6860
Logger("mustache").debug("load template: " + rootPath + template)
6961

7062
val factory = if(Play.isProd) mf else createMustacheFactory
7163

7264
val input = Play.current.resourceAsStream(rootPath + template + ".html").getOrElse(throw new Exception("mustache: could not find template: " + template))
7365
val mustache = factory.compile(new InputStreamReader(input), template)
74-
factory.putTemplate(template, mustache)
66+
//factory.putTemplate(template, mustache)
7567

7668
mustache
7769
}
@@ -80,9 +72,8 @@ class JavaMustache extends MustacheAPI{
8072
Logger("mustache").debug("Mustache render template " + template)
8173

8274
var mustache = {
83-
8475
if(Play.isProd) {
85-
val maybeTemplate = mf.getTemplate(template)
76+
val maybeTemplate = mf.compile(template)
8677
if(maybeTemplate == null) {
8778
readTemplate(template)
8879
} else maybeTemplate
@@ -101,21 +92,18 @@ class JavaMustache extends MustacheAPI{
10192
private[jba] def loadAllTemplate: Unit = {
10293
Logger("mustache").info("Load all mustache template")
10394

104-
val tmplListURL: Option[java.net.URL] = Play.current.resource("mustache/mustache.tmpl")
105-
106-
tmplListURL.map { url =>
95+
Play.current.resource("mustache/mustache.tmpl").map { url =>
10796
for(fileName <- Source.fromFile(url.getFile()).getLines)
10897
readTemplate(fileName)
10998
}.getOrElse {
11099
Logger("mustache").error("Impossible to read file mustache/mustache.tmpl")
111100
}
112101
}
113-
114102
}
115103

116104
object Mustache {
117105

118-
private def plugin = play.api.Play.maybeApplication.map{app =>
106+
private def plugin = play.api.Play.maybeApplication.map { app =>
119107
app.plugin[MustachePlugin].getOrElse(throw new RuntimeException("you should enable MustachePlugin in play.plugins"))
120108
}.getOrElse(throw new RuntimeException("you should have a running app in scope a this point"))
121109

project-code/project/Build.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import play.Project._
55
object ApplicationBuild extends Build {
66

77
val appName = "play2-mustache"
8-
val appVersion = "1.1.1"
8+
val appVersion = "1.1.2"
99

1010
val appDependencies = Seq(
11-
"com.github.spullara.mustache.java" % "compiler" % "0.8.2",
12-
"com.twitter" %% "util-core" % "6.2.0", // For Twitter handler
11+
"com.github.spullara.mustache.java" % "compiler" % "0.8.11",
12+
//"com.twitter" %% "util-core" % "6.2.0", // For Twitter handler
1313
"commons-lang" % "commons-lang" % "2.6"
1414
)
1515

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<h3>{{title}}</h3>

sample/app/controllers/Application.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ object Application extends Controller{
2626

2727
def show = Action {
2828
val content = Content(1, "first", users)
29-
Ok(views.html.show(contents))
29+
Ok(views.html.show(contents))
3030
}
3131

3232
def json = Action {

sample/app/models/Model.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package models
22

33
/**
4-
* Thanx to
4+
* Thanks to
55
* http://mandubian.com/
66
*/
77

sample/project/Build.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import org.jba.sbt.plugin.MustachePlugin._
66

77
object ApplicationBuild extends Build {
88

9-
val appName = "play2-mustachedemo"
9+
val appName = "play2-mustache-demo"
1010
val appVersion = "1.0-SNAPSHOT"
1111

1212
val appDependencies = Seq(
1313
// Add your project dependencies here,
14-
"org.jba" %% "play2-mustache" % "1.1.1",
15-
"com.twitter" %% "util-core" % "6.2.0" // For Twitter handler
14+
"org.jba" %% "play2-mustache" % "1.1.2"
15+
//"com.twitter" %% "util-core" % "6.2.0" // For Twitter handler
1616
)
1717

1818
val main = play.Project(appName, appVersion, appDependencies).settings(

sample/test/BasicTest.scala

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package test
2+
3+
import org.specs2.mutable._
4+
5+
import play.api.test._
6+
import play.api.test.Helpers._
7+
8+
import org.jba.Mustache
9+
10+
import models._
11+
12+
class BasicTestSpec extends Specification {
13+
14+
case class Header(title: String)
15+
16+
"Simple template" should {
17+
18+
"compile" in new WithApplication {
19+
val htmlRes = Mustache.render("title", Header("Title"))
20+
htmlRes.toString.equals("<h3>Title</h3>")
21+
}
22+
23+
"compile with map parameters" in new WithApplication {
24+
val params = Map('title -> "Title")
25+
val htmlRes = Mustache.render("title", params)
26+
htmlRes.toString.equals("<h3>Title</h3>")
27+
}
28+
29+
"wrong template" in new WithApplication {
30+
Mustache.render("title-wrong", Header("Title")) must throwA[Exception]
31+
}
32+
}
33+
34+
"Partial Template" should {
35+
"compile" in new WithApplication {
36+
37+
val user = User("alice@mail.com", "alice")
38+
val user2 = User("bob@mail.com", "bob")
39+
val users = List(user, user2)
40+
val content = Content(1, "first", users)
41+
42+
Mustache.render("content_partial_item", content).toString must contain("alice")
43+
}
44+
}
45+
46+
}

src/main/scala/org/jba/SbtMustachePlugin.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ object MustachePlugin extends sbt.Plugin {
2626
import scala.util.control.Exception._
2727

2828
val jsSource = org.jba.sbt.plugin.MustacheCompiler.compile(mustacheFile, options)
29-
3029
// Any error here would be because of Mustache, not the developer;
3130
// so we don't want compilation to fail.
3231
// val minified = catching(classOf[CompilationException])

0 commit comments

Comments
 (0)