General questions on the Scalate project.
Scalate stands for Scala Template Engine hence ScalaTE, though its easier on the eye to just write 'Scalate'
Details of why you should use Scalate here
See the Getting started Guide :).
Absolutely, you can use Scaml, SSP, Jade or Mustache easily from any web application or J2EE application; just create a WAR, add the scalate-core jar and its dependencies and define the Scalate Filter in your web.xml and you're good to go.
Here's a sample web.xml that has Jersey (JAXRS) and Scalate. You can always take out jersey from your web.xml if you don't want to use JAXRS.
Scalate can work with any objects on the JVM; whether they were developed in Java, Scala, Groovy, JRuby, Clojure or whatever.
The easiest way to get started is by following the Getting started Guide.
See which template engine you should use
The license is Apache 2 License
See the support guide for more details.
First get the source then see the building guide
The default profile in maven does not include remote repos, so make sure you add the download profile when building for the first time. First get the source then see the building guide.
e.g.
mvn install -Pdownload
We currently support both a Maven and SBT build. We tend to use Maven for doing signed releases and reporting and SBT for incremental compiling and testing when in development.
If you want to just use SBT then you first need to perform a maven build to download all the dependencies
mvn install -Pdownload
The SBT build then uses the dependencies downloaded in your local maven repository. This saves us having to maintain duplicate information for dependency versions and repositories across both builds; the SBT build just reuses the dependencies and repositories from Maven. One day it would be nice for SBT to just be able to parse the pom.xml and find that information for itself, so folks won't have to do a Maven build first.
For more detail see the building guide.
We love contributions! More details on how to contribute and how to become a committer are in the contributing guide.
For more detail see the support page. If you haven't done so already you can register with the issue tracker here
For details on how to edit the website and how it works see How the Site works
See the how to choose which template language
Well it all depends really; see this article for a more in depth debate on the subject.
If you don't like to put any code in a view then maybe start with the mustache template language.
See our comparison of Scalate to JSP
We compare approaches here, see which is right for you and your team.
Try the Getting Started Guide which should have you scalating nicely really quickly!
By default Scalate will safely escape any markup characters to avoid you creating invalid or unsafe markup.
However sometimes you want to output a Scala expression which is a String but contains markup you wish to preserve and not escape.
Firstly escaping is enabled by default via the escapeMarkup property on TemplateEngine; as you create a RenderContext it inherits the setting. So you can globally disable escaping by just setting the flag on the TemplateEngine.
Or you can enable/disable it inside a template.
<% escapeMarkup = false %>
The other option is to wrap your expression inside the escape or unescape methods.
e.g.
some text
${unescape("<h1>hello there</h1>")}
Incidentally we assume NodeSeq from Scala's XML API is already markup so we don't escape it. So if you have an expression which is a NodeSeq then it won't be escaped.
<% val markup = <h1>hello<h1> %>
<% val text = "<h1>hello<h1>" %>
this will not be escaped ${markup}
this will be escaped ${text}
If you want to disable layouts, just set the layoutStrategy property on the TemplateEngine to be the NullLayoutStrategy
Or you can just create an empty layout. For example if you are using servlets or JAXRS, just create a template in /WEB-INF/scalate/layouts/default.ssp which just includes the body and does not perform any actual layout.
Scalate itself auto-recompiles and reloads templates on the fly. However if you are changing Scala code which the Scalate templates use then you normally have to stop and restart your web container.
So for a more RAD approach we recommend using JRebel which can hot-reload the Scala code on the fly, leading to a rapid edit ←> browser reload cycle.
When using Scalate with servlets or Jog you might wish to access the current request, response, servletContext or servletConfig objects used by the servlet engine. These variables are all available by default thanks to the ServletRenderContext API which is imported by default.
So your template can refer to these variables if they are required directly - rather like they can be in JSP.
<h1>Hello ${someMethod(request, response)}</h1>
where someMethod is a function takes a Servlet request and response parameter.
However if you are using 1.1 or later you can avoid having to mention these variables in your templates; instead your snippet functions can refer to these values directly (or you can write snippet functions that hide these parameters to simplify your templates)
You can configure the TemplateEngine to provide a resourceLoader to resolve URIs for templates. See the Custom Template Loading in the Embedding Guide.
Or if you are using the Scalate API directly there are methods on TemplateEngine API for loading a Template by supplying the source of the template as a String. So you can create the template content however you wish, then compile and render the template.
For example
// a template with an attribute
val source = "<%@ val name: String %> hello ${name}"
val template = engine.compileSsp(source)
val output = engine.layout(template, Map("name" -> "James"))
// test it works...
expect("hello James") {output}
Or you can use the TemplateSource companion object helper methods…
// can use fromString, fromUri etc
val source = TemplateSource.fromFile("foo.ssp")
val output = engine.layout(source, Map("name" -> "James"))
To see more examples try the TemplateEngineTest.scala test case
See the section on precompiling templates in the user guide
The list of default layout templates is a constructor parameter in the DefaultLayoutStrategy class; its configured by default in the ServletTemplateEngine.setLayoutStrategy(templateEngine) method.
Though you can just specify whatever defaults you wish if you are using some framework to configure the TemplateEngine instance (whether using a regular TemplateEngine or the ServletTemplateEngine)
val engine = new ServletTemplateEngine
engine.layoutStrategy = new DefaultLayoutStrategy(engine,
"something.ssp", "whatnot/somethingElse.jade")
The other option is to define the layout attribute to be whatever layout template you wish before rendering your template.
In template engines like JSP and GSP its common to use XML tag libraries. Its a pending support request which we might implement in SSP one of these days.
Though typically its easier to just use a Scala method call in Scalate rather than mapping namespaced XML elements to functions.
Refer to the user guide on calling functions and passing blocks for some samples. An example of this kind of thing is performing a layout of a template block