8
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ I would like to add "playframework" as a new tag... but don't have the right to do it... Could someone with such power create it for me, or better, give me the right to do it? \$\endgroup\$ – blackbox Oct 17 '11 at 6:38
  • \$\begingroup\$ have you considered anti-XML? anti-xml.org I guess no one answered this question yet because the Scala XML standard package is outdated and beyond fixing. \$\endgroup\$ – Marvin.Hansen Mar 15 '14 at 19:19
  • \$\begingroup\$ Thanks for pointing antiXML... I'll be glad to test it when I have time. \$\endgroup\$ – blackbox Mar 17 '14 at 10:56
6
\$\begingroup\$

I'm not that familiar with Play, but overall I can't say that there's anything here I actively dislike. The purpose, layout, variable names and all are quite reasonable and easy to follow. Over all, I would say "Good job".

A couple of comments outside of that:

  1. It seems strange that you would have to write all this boiler-plate HTML stuff in code. My preference would be to keep a template, but Your Mileage May Vary.

  2. I can't say that I'm a huge fan of using concatenation operators to create static lists. I would have, for example, written:

    val cssIncludes = List("main.css",
                           "jquery-ui-1.8.16.custom.css",
                           "dynaTree/skin/ui.dynatree.css")
    

    It isn't that the way you wrote it is wrong, but this form is exactly equivalent and more obvious (and thus more readable).

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.