[2.0] How to use Scala templates to produce static html files

312 views
Skip to first unread message

computerpunc

unread,
Aug 23, 2012, 8:25:00 AM8/23/12
to play-fr...@googlegroups.com
I would like to use the scala template engine to produce static html files.
My flow is a simple one: I'll have in a directory with xyz.scala.html files, I then run a command on the sbt to compile the files and produce xyz.html files in another directory. Not all xyz.scala.html files would produce an equivalent file. Only the ones that do not take any parameters, i.e., take the form of @() at the beginning of file.

The motivation for all of this is to use the templating engine to produce static html files to be uploaded to static hosting provider (S3 in my case).

Any idea how to tackle this issue would be greatly appreciated.

CPC.

Will Sargent

unread,
Aug 23, 2012, 2:58:46 PM8/23/12
to play-fr...@googlegroups.com
On Thu, Aug 23, 2012 at 5:25 AM, computerpunc <comput...@gmail.com> wrote:
> I would like to use the scala template engine to produce static html files.
> My flow is a simple one: I'll have in a directory with xyz.scala.html files,
> I then run a command on the sbt to compile the files and produce xyz.html
> files in another directory. Not all xyz.scala.html files would produce an
> equivalent file. Only the ones that do not take any parameters, i.e., take
> the form of @() at the beginning of file.
>
> The motivation for all of this is to use the templating engine to produce
> static html files to be uploaded to static hosting provider (S3 in my case).

You would probably be able to do this by changing the template engine
under the hood.

Here's Jan Helwich's work to make Play use Scaml -- you can tweak it
to write out to a file as well, and then send the framework requests
for the files you want.

http://janhelwich.wordpress.com/2012/03/24/play-2-0-with-scala-and-scaml-part-2-setup-example-data-at-startup-and-integrating-scalate/

https://github.com/janhelwich/Play-2-with-Scala-and-Scalate

Will.

Max

unread,
Aug 24, 2012, 1:46:59 AM8/24/12
to play-fr...@googlegroups.com
Have you encountered performance bottleneck in run time? If yes, what's your volume?

A easy workaround is to cache pages. 

computerpunc

unread,
Aug 27, 2012, 7:33:57 AM8/27/12
to play-fr...@googlegroups.com
There's no performance bottleneck. It's just that my current deployment (for a specific project) is on S3 and in the future I may use a CDN.

computerpunc

unread,
Aug 27, 2012, 7:47:20 AM8/27/12
to play-fr...@googlegroups.com
Thanks for the tips.

I chose the following solution to solve my problem:

1. Have a dynamic route to serve xyz.scala.html files.
* In routes I added: GET     /*file                      controllers.Application.serveFile(file)
* with a corresponding serveFile method (that mimics S3 static website behaviour):
def serveFile(pathToFile:String)=Action { request=>
    val objectName:String="views.html."+(pathToFile.matches(".+\\.html$") match {
      case true=> pathToFile.replaceAll("\\.html$","").replace('/', '.')+"$"
      case _=> pathToFile.replaceAll("/$", "").replace('/', '.')+".index$"
    })
    try {
      val fileObject=Class.forName(objectName).getField("MODULE$").get(null).asInstanceOf[{ def apply() : play.api.templates.Html }]
      Ok(fileObject())
    } catch {
      case _=> NotFound
    }
  }

2. I added a script to extract files while play is running. The script reads the list of resources to extract from a file (site-files.txt):
#!/bin/bash

if [ "x$1" = "x" ]; then
  echo "Need to Specify the directory to extract to"
  exit 1
fi

FILE="conf/site-files.txt"

while read LINE; do
FILENAME=$(basename $LINE)
PATHNAME=${LINE%%$FILENAME}
rm $1$LINE
wget -nv -P $1$PATHNAME http://localhost:9988$LINE
done < "$FILE"


CPC.
Reply all
Reply to author
Forward
0 new messages