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.

Vogel612
25.5k7 gold badges59 silver badges141 bronze badges
asked Oct 17, 2011 at 6:38
\$\endgroup\$
3
  • \$\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\$ Commented Oct 17, 2011 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\$ Commented Mar 15, 2014 at 19:19
  • \$\begingroup\$ Thanks for pointing antiXML... I'll be glad to test it when I have time. \$\endgroup\$ Commented Mar 17, 2014 at 10:56

1 Answer 1

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

answered Sep 22, 2014 at 12:18
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.