Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Shootout! Template engines for the JVM

Jeroen Reijn
November 06, 2013

Shootout! Template engines for the JVM

These slides were from my JFall 2013 presentation about new template engines for the JVM. During this presentation I gave a short introduction to new and interesting template engines and show how they compare to the most well known: JSP, Velocity and Freemarker.

Jeroen Reijn

November 06, 2013
Tweet

More Decks by Jeroen Reijn

Other Decks in Programming

Transcript

  1. About me • Architect @ Hippo • Worked on web

    based platforms since 2001 with PHP, XSLT, JSP, Velocity and Freemarker • Blogger at http://blog.jeroenreijn.com
  2. Template engine? • What is a Template ? • What

    is a Template Engine ? • How many Java Template Engines ? • Java-Source.net : 21 • Wikipedia: 17
  3. Selection criteria • Project activity • Framework support • Active

    Community • Brings added value • IDE support
  4. Overview • Available at http://www.thymeleaf.org/ • Java, DOM based template

    engine • XML / XHTML / HTML5 • Full Spring MVC integration • ‘Natural templating’
  5. 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”
  6. 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, Objective- C, .NET, C++, Go, ColdFusion, Scala, etc.
  7. Overview • Java implementations: • JMustache • Mustache.java • Not

    web specific • Spring MVC integration as an 3th party add-on
  8. Sections { "repo": [ { "name": "resque" }, { "name":

    "hub" }, { "name": "rip" }, ] } {{#repo}} <li>{{name}}</li> {{/repo}} Data Template <li>resque</li> <li>hub</li> <li>rip</li> Output
  9. Jade • Available at http://jade-lang.com • Node.js Template engine •

    Java Implementation: Jade4j • Compact notation
  10. 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, ...
  11. Scaml • Based on HAML (HTML abstraction markup language) •

    Integrates Scala code • Well formatted markup • Similar to Jade
  12. Benchmark project • Simple Spring MVC application • Multiple view

    resolvers • Page that show a list of presentations • Uses page layouts / fragments • Uses i18n where possible
  13. public class Presentation { private Long id; private String title;

    private String speakerName; private String summary; private String room; private Date startTime; private Date endTime; }
  14. Project available • Project will be available as open source

    on github. • https://github.com/jreijn/spring-comparing- template-engines
  15. Performance 0,00s 3,57s 7,14s 10,71s 14,29s 17,86s 21,43s 25,00s Total

    time taken for 25.000 requests (less is better) 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
  16. Findings • Thymeleaf uses 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!
  17. 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