Scalate support
Scalate is a template engine written in scala. It supports two dialects: ssp (jsp for scala) and scaml (haml for scala). Play provides integration for both via a special Controller class.
Getting started
first you need to grab the release from the modules repository:
play install scalate-0.2
Starting a New Project
then if you start a new project all you need to do is:
play new myapp --with scala,scalate
(if you have any other version installed from scala or scalate than 0.1, then you will need to explicitly reference both ie. --with scalate-0.2,scala-0.1)
the scalate module is depending on the scala module, so both modules need to be installed and activated
Existing Projects
or if you want to configure an existing scala project, you will need to edit your conf/application.conf:
module.scalate=${play.path}/modules/scalate-0.1
and set the dialect:
scalate=ssp
or
scalate=scaml
then you will also need to increase the heap size:
jvm.memory=-Xmx256M -Xms32M
finally you will need to change your controllers to inherit from play.mvc.ScalateController
Example
object Application extends ScalateController {
def index(name: String = "Guest user") = renderScalate(name) //renderArgs("name",name) also works
}
the corresponding template could look something like this:
<%@ var name: String %>
Hello ${name}
if the method above is invoked, play will render app/views/Application/index.ssp with the following context variables already populated:
- name
- request
- flash
- session
- parameters
and the following packages will be imported by default:
- models._
- controllers._
- play.utils._
This means that you will need to have at least one class in each of these packages, otherwise templates can not be compiled
Precompile templates
play scalate:precompile
the command above will pregenerate all templates into the tmp folder
It’s worth noting that by default the precompiler is scanning only the first 20 lines of each template for variable declaration (ie <%@ var %> )
Difference between production and dev mode
in dev mode, play will alow instant template reloading, that is, if you make a change to a template and refresh the page, the change will apear. In production, however, template reloading is disabled for performance reasons.
Layout
scalate now supports layouts, partials as well. Play stores default layouts in app/views/default.ssp (or app/views/default.scaml).
Reverse URL
<%= url("Application.index") %>
this will give back the index page
<%= url("Application.something", Map("id"->5)) %>
this will resolve method “Application#something” with argument “id”
<%= absoluteurl("Application.index") %>
this will resolve the full url
<%= staticurl("/public/images/favicon.png") %>
this will resolve the full url for the favicon asset.
demo
a sample application is bundled with the module and you can find it under samples-and-tests/simpleapp.