Shootout! template engines on the jvm

4,359 views

Published on

Published in: Technology

Shootout! template engines on the jvm

  1. 1. Shootout! Template engines on the JVM Jeroen Reijn | @jreijn | #jfall13
  2. 2. About me • Architect @ Hippo • Worked on web based platforms since 2001 with PHP, XSLT, JSP,Velocity and Freemarker • Blogger at http://blog.jeroenreijn.com
  3. 3. Session agenda • General overview • Interesting template engines • Benchmark project • Performance results
  4. 4. Template engine? • What is a Template ? • What is a Template Engine ? • How many Java Template Engines ? • Java-Source.net : 21 • Wikipedia: 17
  5. 5. Most commonly used • JSP • Freemarker • Velocity
  6. 6. Overview
  7. 7. Selection criteria • Project activity • Framework support • Active Community • Brings added value • IDE support
  8. 8. Most interesting • Thymeleaf • Mustache • Jade • Scalate
  9. 9. Thymeleaf
  10. 10. Overview • Available at http://www.thymeleaf.org/ • Java, DOM based template engine • XML / XHTML / HTML5 • Full Spring MVC integration • ‘Natural templating’
  11. 11. Natural templating • From Wikipedia: “The template can be a document as valid as the final result, the engine syntax doesn't break the document's structure”
  12. 12. JSP in browser
  13. 13. Thymeleaf in browser
  14. 14. Variables & Loops • Variables • Loops
  15. 15. Fragments • Include fragment • Define fragment based on th:fragment
  16. 16. {{ Mustache }}
  17. 17. Overview • Available at https://mustache.github.com • “logic-less” because there are no control statements (i.e. if, else, loops, etc) • Tags are specified using {{ }} (looks like a mustache) • Implemented by many languages: Java, Ruby, JavaScript, Python, Erlang, PHP, ObjectiveC, .NET, C++, Go, ColdFusion, Scala, etc.
  18. 18. Overview • Java implementaions: • JMustache • Mustache.java • Not web specific • Spring MVC integration as an add-on
  19. 19. Variables Data { } “greeting” : “Hello”, “message” : “World” Template {{greeting}} {{message}} Output Hello World
  20. 20. Sections Data { "repo": [ { "name": "resque" }, { "name": "hub" }, { "name": "rip" }, ] } Template {{#repo}} <li>{{name}}</li> {{/repo}} Output <li>resque</li> <li>hub</li> <li>rip</li>
  21. 21. Fragments Template base.mustache: <h2>Presentations</h2> {{#presentations}} {{> presentation}} {{/presentations}} presentation.mustache: <h3>{{title}}</h3> <p>{{startTime}}</p>
  22. 22. Jade
  23. 23. Jade • Available at http://jade-lang.com • Node.js Template engine • Java Implementation: Jade4j • Compact notation
  24. 24. Syntax
  25. 25. Result
  26. 26. Variables & Iterations • Variables • Iterations
  27. 27. Fragments
  28. 28. Overview • Available at http://scalate.fusesource.org/ • Scala based Template Engine (ScalaTE) • Templates are compiled as Scala objects • Equivalent to using JSP, JSTL, JSP EL & Tiles / Sitemesh • Integration available for Spring, Play, Lift, ...
  29. 29. Template languages • SSP (Scala Server Pages) • SCAML (based on Ruby’s HAML) • Mustache • Jade
  30. 30. Scaml • Based on HAML (HTML abstraction markup language) • Integrates Scala code • Well formatted markup • Similar to Jade
  31. 31. Variables & Loops • Fragment support is possible • Inline Scala code
  32. 32. Benchmark project
  33. 33. Benchmark project • Simple Spring MVC application • Multiple view resolvers • Page that show a list of presentations • Uses page layouts / fragments • Uses i18n where possible
  34. 34. Spring MVC
  35. 35. public class Presentation { private Long id; private String title; private String speakerName; private String summary; private String room; private Date startTime; private Date endTime; }
  36. 36. Project available • Project will be available as open source on github. • https://github.com/jreijn/spring-comparingtemplate-engines
  37. 37. Results!
  38. 38. Performance 30,00s Thymeleaf - v2.1.0 Jade4j - v0.3.17 Scalate - v1.6.1 Mustache - v1.8 Freemarker - v2.3.19 Velocity - v1.7 JSP - 2.1 26,50s 23,00s 19,50s 16,00s 12,50s Total time taken for 25.000 requests (less is better)
  39. 39. Findings • Thymeleaf used more memory and was slower then the commonly used • Scalate was a lot slower due to the layout mechanism more than 100 seconds for 25.000 requests. • Mustache performs really well
  40. 40. Tips • If you want designers to own your templates consider Mustache or Thymeleaf • If you want developers to own your templates: • If you want to generate HTML / XML and DRY? => Jade • otherwise: JSP, Freemarker, etc
  41. 41. Thank you! Questions? @jreijn j.reijn@onehippo.com

×