introducing scalate, the scala template engine

27
Copyright © 2010 Progress So3ware Corpora6on and/or its subsidiaries or affiliates. All rights reserved. A Progress So3ware Company A Progress So3ware Company Scalate Introducing the Scala Template Engine James Strachan, FuseSource 1 Thursday, June 16, 2011

Upload: strachaj

Post on 18-Jan-2015

6.452 views

Category:

Technology


2 download

DESCRIPTION

Keynote I gave at Scala eXchange in London, June 2011

TRANSCRIPT

Page 1: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

A  Progress  So3ware  Company

ScalateIntroducing  the  Scala  Template  EngineJames  Strachan,  FuseSource

1

Thursday, June 16, 2011

Page 2: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Your  presenter  is:  James  Strachan

James  Strachan• [email protected]• twi1er:  @jstrachan• blog:  h1p://macstrac.blogspot.com/

So3ware  Fellow  at  FuseSource long  term  Open  Source  contributor

• created  Apache  Camel• created  the  Groovy  programming  language• co-­‐founder  of–Apache  Ac6veMQ,  Camel,  ServiceMix,  Fabric,  ...–Scalate

2

Thursday, June 16, 2011

Page 3: Introducing Scalate, the Scala Template Engine

 FuseSource  sponsors  projects  at  the  Apache  So3ware  Founda6on

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

FuseSource  -­‐  experts  in  open  source  integra6on  and  messaging

3

CXF

ActiveMQ

Camel

ServiceMix

JAX-WS, WS-*, RESTfor Web and Restful Integration

Enterprise Messaging,multi platform and language support,

JMS,STOMP,AMQP1.0,Highly available and fault tolerant

Enterprise Integration Patterns framework

Enterprise Service Bus,distributed,clustered, OSGi, JBI

Karaf OSGi based container, supporting multiple archive types, remote

management, dynamic configuration, extensible shell

Thursday, June 16, 2011

Page 4: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Some  recommended  books...

Thursday, June 16, 2011

Page 5: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Agenda

scala  and  mescalate  overviewgeQng  startedQ  &  A

5

Thursday, June 16, 2011

Page 6: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Scala  and  me

6

Thursday, June 16, 2011

Page 7: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Scalate  Overview

its  a  Scala  based  Template  Engine• Scala    T  E  =>  scalate

h1p://scalate.fusesource.org/

7

Thursday, June 16, 2011

Page 8: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Design  Goals

make  an  awesome  template  engine  • framework  &  container  agnosJc

support  different  template  flavours  under  same  API• there  isn’t  really  a  one  size  fits  all

reuse  the  power  &  type  safety  of  Scala• catch  errors  in  templates  at  edit/build  Jme– templates  are  for  life,  not  just  for  Christmas!

• IDE  friendly  for  smart  compleJon

8

Thursday, June 16, 2011

Page 9: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Features

Templates  are  compiled  &  cached  as  fast  Scala  objects

Template  objects  are  staJcally  typed  to  catch  typos• apart  from  Mustache

Scalate  is  equivalent  to  the  combinaJon  of  JSP,  JSTL,  JSP  EL  &  SiteMesh/Tiles• Scala  replaces  JSP  EL  and  Scala  funcJons  replace  JSP  custom  tags

• Scalate  template  languages  are  richer,  more  powerful  &  usually  more  DRY  than  JSP  +  JSTL

• Scalate  has  in  built  layout  support  replacing  SiteMesh/Titles

9

Thursday, June 16, 2011

Page 10: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

How  Scalate  works

TemplateEngine  is  used  to  load  Templates• typically  templates  are  converted  into  scala  code  and  compiled  to  avoid  runJme  parsing

• can  customize  various  things...– caching  &  reloading– how  URIs  are  resolved  (classpath,  file  system  etc)

– interna6onalisa6on

RenderContext  is  used  to  render  a  template• has  a  Map[String,Any]  like  set  of  a1ributes  for  passing/sharing  state

• loads  of  helper  methods  for  use  inside  the  template– localisa6on,  including  other  templates,  views,  layouts  etc

template  languages  implemented  using  Scala  parser  generators

10

Thursday, June 16, 2011

Page 11: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Scalate  Template  Languages

SSP

Scaml

Jade

Mustache

Scuery

11

Thursday, June 16, 2011

Page 12: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

SSP

SSP  “Scala  Server  Pages”  • like  a  non-­‐sucky  JSP  :)

easiest  template  engine  to  start  with  if  folks  know• ASP,  JSP,  GSP,  Erb,  Velocity,  FreeMarker  style  templates

supports  JSP  /  Erb  style  syntax  • <%  statements  %>

• <%=  expression  %>

• ${expression}

examples:• Dear  ${people.map(_.name).mkString(“,  “)}  how  are  you?

12

Thursday, June 16, 2011

Page 13: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

SSP  also  supports  Velocity  style  tokens

#if  (lineItems.find(_.price  >  10).isDefined)

   <b>Welcome  big  spender!</b>

#end

13

Thursday, June 16, 2011

Page 14: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Scaml

Scaml  is  a  Scala  port  of  HAML• HAML  started  in  the  Rails  world  and  became  a  very  popular  alternaJve  to  Erb  templates

• whitespace  sensiJve  (which  is  handy  for  markup,  avoids  badly  nested  markup)

• uses  %foo  to  indicate  <foo>...</foo>

• -­‐  for  statements

• =  expressions

14

Thursday, June 16, 2011

Page 15: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Jade

Jade  out  in  JavaScript  /  Rails  worlds  as  a  simpler  more  DRY  version  of  HAML  /  Scaml  • avoid  the  %  to  indicate  element  name– folks  tend  to  use  lots  of  elements;  few  are  mul6-­‐line  text  content

• use  |  to  indicate  text  content  inside  an  element  when  mulJple  lines  required

• Implemented  as  just  a  slightly  different  surface  parser  on  Scaml

Jade  +  markdown  are  the  hotness!

15

Thursday, June 16, 2011

Page 16: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Mustache

no  logic  in  the  scripts  

can  be  used  from  JavaScript  on  the  client  side  or  Scala  /  Scalate  on  the  server  side

uses  {{foo}}  mustaches  for  values

use  {{#foo}}  and  {{/foo}}  for  blocks/loops/condiJonals

good  if  you  want  your  designer  to  own  the  templates• though  dynamically  typed

16

Thursday, June 16, 2011

Page 17: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Scuery

more  of  a  HTML/XHTML  transformaJon  engine  really

uses  CSS3  parser  &  transformaJon  engine

templates  are  regular  HTML  pages  (possibly  with  mock  data)  owned  by  your  designer

use  the  transform  API  (jQuery-­‐ish)  to  transform  the  HTML  to  inject/replace  with  dynamic  data

17

Thursday, June 16, 2011

Page 18: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Tips  on  picking  a  template  language

If  you  want  your  designers  to  own  your  templates• consider  mustache  or  scuery

if  you  want  developers  to  hack  your  templates• want  to  generate  HTML/XML  really  DRY?  – use  jade

• otherwise  SSP

18

Thursday, June 16, 2011

Page 19: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Scalate  Layouts

TemplateEngine.layoutStrategy  defines  the  strategy

default  implementaJon  uses  the  “layout”  a1ribute  to  define  the  template  used  for  laying  out  any  template

to  change  the  layout  you  want  to  use...• -­‐  a1ributes(“layout”)  =  “/my/layouts/foo.jade”

to  disable  layouts• -­‐  a1ributes(“layout”)  =  “”

19

Thursday, June 16, 2011

Page 20: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Using  Scalate

can  use  TemplateEngine  directly  if  doing  code  generaJon

in  web  applicaJons  add  this  to  your  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>

20

Thursday, June 16, 2011

Page 21: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Framework  support

bowler

jersey

lit

playframework

scalatra

servlets

sbt  /  lity

spring  mvc

unfiltered

21

Thursday, June 16, 2011

Page 22: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

mod  your  TemplateEngine!

package scalate

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

class Boot(engine: TemplateEngine) {

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

22

Thursday, June 16, 2011

Page 23: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Precompile  your  templates

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

23

Thursday, June 16, 2011

Page 24: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

GeQng  Started

install  Scalate• h1p://scalate.fusesource.org/download.html

• add  SCALATE_HOME/bin  to  your  PATH

scalate  create  jersey  mygroup  myarJfact

cd  myarJfact

mvn  je1y:run

24

Thursday, June 16, 2011

Page 25: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Sta6c  site  genera6on

maven  plugin  to  generate  a  complete  staJc  website  for  your  project  using  Scalate

deploy  it  to  some  web  server  using  maven’s  wagon  transports• scp  /  webdav  etc

allows  real  Jme  updates  of  templates  &  layouts  while  ediJng  docs

your  own  personal  offline  wiki  using  git  for  source  control  &  versioning• use  a  text  editor  to  edit,  erm,  text  files!  :)

25

Thursday, June 16, 2011

Page 26: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Demo  6me!

26

Demo  templates  &  web  app  here:hhps://github.com/scalate/scalate/tree/master/samples/scalate-­‐example

Thursday, June 16, 2011

Page 27: Introducing Scalate, the Scala Template Engine

Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  Company

Any  Ques6ons?

27

twiher:  @jstrachan

hhp://fusesource.com

h1p://scalate.fusesource.org/

Thursday, June 16, 2011