I am currently exploring the play framework. I'm about to replace the proposed templating system, using the powerful XML processing of the Scala library. Here is what I have come with:
import scala.xml._
import play.templates._
import play.mvc.results.ScalaAction
object Gui {
def asset (file:String) = "/public/"+ file
val cssIncludes = "main.css" ::
"jquery-ui-1.8.16.custom.css" ::
"dynaTree/skin/ui.dynatree.css" ::
Nil
val jsIncludes = "jquery-1.6.2.min.js" ::
"jquery.cookie.js" ::
"jquery-ui-1.8.16.custom.min.js"::
"jquery.dynatree.min.js" ::
Nil
def pageBase(title: String = "", jsScript: Option[String])(body: => Seq[Node]) = {
<html>
<head>{
val nodes :Seq[Node] = <title>{ title }</title>::
(for (css <- cssIncludes) yield
<link rel="stylesheet" href={ asset("stylesheets/" + css) }></link>
):::
(for (js <- jsIncludes) yield
<script src={asset("javascripts/" + js)} type="text/javascript"></script>
):::(
<link rel="shortcut icon" type="image/png" href="public/images/favicon.png"></link>
<script type="text/javascript">{jsScript getOrElse ""}</script>).toList
nodes
}</head>
<body>{ body }</body>
</html>
}
}
This seems to work well and can be a base for more complex needs.
I would be interested in a way to improve the pageBase
method. I did not find a clean way to generate the stylesheet and JavaScript inclusion without the :::
operator. (I managed to get the code to compile, but only the last for expression would yield a result at execution)
Would you share a better way to write it, or show your implementation, if you took a similar path?
I'm still searching a better way to handle path creation (for asset and action) that mimics the routing and reverse routing offered in the templates.