HiveBrain v1.2.0
Get Started
← Back to all entries
patternMinor

Basic XML template

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
templatexmlbasic

Problem

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]) = {
    
      {
        val nodes :Seq[Node] = { title }::
          (for (css 
          ):::
          (for (js 
          ):::(
         
        {jsScript getOrElse ""}).toList
        nodes
      }
      { body }
    
  }
}


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.

Solution

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:

-
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.

-
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).

Code Snippets

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

Context

StackExchange Code Review Q#5411, answer score: 6

Revisions (0)

No revisions yet.