Introducing Scalate, the Scala Template Engine

8,213 views

Published on

Keynote I gave at Scala eXchange in London, June 2011

Published in: Technology
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
8,213
On SlideShare
0
From Embeds
0
Number of Embeds
32
Actions
Shares
0
Downloads
79
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Introducing Scalate, the Scala Template Engine

  1. 1. Scalate Introducing  the  Scala  Template  Engine James  Strachan,  FuseSource A  Progress  So3ware  Company 1 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  2. 2. Your  presenter  is:  James  Strachan  James  Strachan • james@fusesource.com • 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  3. 3. FuseSource  -­‐  experts  in  open  source  integra6on  and  messaging   FuseSource  sponsors  projects  at  the  Apache  So3ware  Founda6on CXF JAX-WS, WS-*, REST for Web and Restful Integration Enterprise Messaging, ActiveMQ multi platform and language support, JMS,STOMP,AMQP1.0, Highly available and fault tolerant Camel Enterprise Integration Patterns framework ServiceMix Enterprise Service Bus, distributed,clustered, OSGi, JBI Karaf OSGi based container, supporting multiple archive types, remote management, dynamic configuration, extensible shell 3 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  4. 4. Some  recommended  books... Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  5. 5. Agenda scala  and  me scalate  overview geQng  started Q  &  A 5 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  6. 6. Scala  and  me 6 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  7. 7. Scalate  Overview  its  a  Scala  based  Template  Engine • Scala    T  E  =>  scalate  h1p://scalate.fusesource.org/ 7 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  8. 8. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  9. 9. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  10. 10. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  11. 11. Scalate  Template  Languages  SSP  Scaml  Jade  Mustache  Scuery 11 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  12. 12. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  13. 13. SSP  also  supports  Velocity  style  tokens #if  (lineItems.find(_.price  >  10).isDefined)    <b>Welcome  big  spender!</b> #end 13 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  14. 14. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  15. 15. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  16. 16. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  17. 17. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  18. 18. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  19. 19. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  20. 20. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  21. 21. Framework  support  bowler  jersey  lit  playframework  scalatra  servlets  sbt  /  lity  spring  mvc  unfiltered 21 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  22. 22. mod  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") } } 22 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  23. 23. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  24. 24. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  25. 25. 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 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  26. 26. Demo  6me! Demo  templates  &  web  app  here: hhps://github.com/scalate/scalate/tree/master/samples/scalate-­‐example 26 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011
  27. 27. Any  Ques6ons? h1p://scalate.fusesource.org/ twiher:  @jstrachan hhp://fusesource.com 27 Copyright  ©  2010  Progress  So3ware  Corpora6on  and/or  its  subsidiaries  or  affiliates.  All  rights  reserved.   A  Progress  So3ware  CompanyThursday, June 16, 2011

×