Scalate: the Scala Template Engine

Scalate

James Strachan
@jstrachan
FuseSource

Your presenter: James Strachan

Scalate Overview

Scalate

Design Goals

Scalate

Features

How Scalate works

Scalate Template Languages

SSP

SSP also supports Velocity style tokens

example

#if (lineItems.find(_.price > 10).isDefined)
  <b>Welcome big spender!</b>
#end

SSP sample page

Sample page using SSP markup

<%
  val people = Map("James" -> "Mells", "Hiram" -> "Tampa")
%>

<h1>Table Example</h1>

<div class="main">
  <table>
    <tr>
      <th>Name</th>
      <th>Location</th>
    </tr>
    #for ((name, location) <- people)
    <tr>
      <td>${name}</td>
      <td>${location}</td>
    </tr>
    #end
  </table> 
</div>

<div class="description">
  <p>
    This is lots and lots of text.
    <b>Scalate</b> really is rather awesome!
  </p>      
</div>

Scaml

Scaml sample page

Sample page using Scaml markup

- val people = Map("James" -> "Mells", "Hiram" -> "Tampa")

%h1 Table Example

.main
  %table
    %tr
      %th Name
      %th Location
    - for ((name, location) <- people) 
      %tr
        %td = name
        %td = location

.description      
  %p
    This is lots and lots of text.
    <b>Scalate</b> really is rather awesome!

Jade

Jade sample page

Sample page using Jade markup

- val people = Map("James" -> "Mells", "Hiram" -> "Tampa")

h1 Table Example

.main
  table
    tr
      th Name
      th Location
    - for ((name, location) <- people) 
      tr
        td= name
        td= location
      
.description      
  :markdown
    This is lots and lots of text.
    **Scalate** really is rather awesome!

Mustache

Mustache sample page

Sample page using Mustache markup

<table>
  <tr>
    <th>Name</th>
    <th>Location</th>
  </tr>
  {{#people}}
  <tr>
    <td>{{name}}</td>
    <td>{{location}}</td>
  </tr>
  {{/people}}
</table> 

<p>
  This is lots and lots of text.
  <b>Scalate</b> really is rather awesome!
</p>      

Scuery

Scuery sample HTML page

Sample page using plain HTML markup

<ul class="people">
  <li>
    <a href="#" class="person">A person</a>
  </li>
  <li>
    <a href="#" class="person">Another person</a>
  </li>
</ul>

Scuery sample Scala transform

Sample HTML page transform with Scuery

import org.fusesource.scalate.scuery._

case class Person(name: String, location: String)


object MyTransform extends Transformer {
  val people = List(Person("James", "Beckington"), Person("Hiram", "Tampa"))
  
  $(".people").contents {
    node =>
      people.flatMap {
        p =>
          transform(node.$("li:first-child")) {
            $ =>
              $("a.person").contents = p.name
              $("a.person").attribute("href").value = "http://acme.com/bookstore/" + p.name
          }
      }
  }
}

Tips on picking a template language

Scalate Layouts

Using Scalate

add this to web.xml

<filter>
  <filter-name>TemplateEngineFilter</filter-name>
  <filter-class>org.fusesource.scalate.servlet.TemplateEngineFilter</filter-class>
</filter>
<filter-mapping>
  <filter-name>TemplateEngineFilter</filter-name>
  <url-pattern>/*</url-pattern>
</filter-mapping>

Framework support

mod your Template Engine!

add this class to pimp your TemplateEngine

package scalate 

import org.fusesource.scalate.TemplateEngine
import java.io.File

class Boot(engine: TemplateEngine) {

  def run: Unit = {
    // lets change the workingDirectory
    engine.workingDirectory = new File("myScalateWorkDir")
  }
}
        

Precompile your templates

add to your pom.xml

<build>
  <plugins>
    <plugin>
      <groupId>org.fusesource.scalate</groupId>
      <artifactId>maven-scalate-plugin</artifactId>
      <version>1.5.0</version>
      <executions>
        <execution>
          <goals>
            <goal>precompile</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
        

Getting Started

Create a sample project

scalate create jersey mygroup myartifact
cd myartifact
mvn jetty:run

To try out the Scalate shell

scalate
help

Static site generation

Create a website project

scalate create sitegen mygroup mysite
cd mysite
mvn jetty:run

Presentation generation

To try it out locally from a scalate checkout

cd samples/scalate-presentation
mvn jetty:run

Demo time!

Cross Fingers

Any Questions?

Scalate

Questions