Slide 1

Slide 1 text

Shootout! Template engines for the JVM Jeroen Reijn | @jreijn | #jfall13

Slide 2

Slide 2 text

About me • Architect @ Hippo • Worked on web based platforms since 2001 with PHP, XSLT, JSP, Velocity and Freemarker • Blogger at http://blog.jeroenreijn.com

Slide 3

Slide 3 text

Session agenda • General overview • Interesting template engines • Benchmark project • Performance results

Slide 4

Slide 4 text

Template engine? • What is a Template ? • What is a Template Engine ? • How many Java Template Engines ? • Java-Source.net : 21 • Wikipedia: 17

Slide 5

Slide 5 text

Most commonly used • JSP • Freemarker • Velocity

Slide 6

Slide 6 text

Overview

Slide 7

Slide 7 text

Selection criteria • Project activity • Framework support • Active Community • Brings added value • IDE support

Slide 8

Slide 8 text

Most interesting • Thymeleaf • Mustache • Jade • Scalate

Slide 9

Slide 9 text

Thymeleaf

Slide 10

Slide 10 text

Overview • Available at http://www.thymeleaf.org/ • Java, DOM based template engine • XML / XHTML / HTML5 • Full Spring MVC integration • ‘Natural templating’

Slide 11

Slide 11 text

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”

Slide 12

Slide 12 text

JSP in browser

Slide 13

Slide 13 text

Thymeleaf in browser

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

Variables & Loops • Variables ! • Loops

Slide 16

Slide 16 text

Fragments • Include fragment ! • Define fragment based on th:fragment

Slide 17

Slide 17 text

{{ Mustache }}

Slide 18

Slide 18 text

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.

Slide 19

Slide 19 text

Overview • Java implementations: • JMustache • Mustache.java • Not web specific • Spring MVC integration as an 3th party add-on

Slide 20

Slide 20 text

Variables { “greeting” : “Hello”, “message” : “World” } {{greeting}} {{message}} Data Template Hello World Output

Slide 21

Slide 21 text

Sections { "repo": [ { "name": "resque" }, { "name": "hub" }, { "name": "rip" }, ] } {{#repo}}
  • {{name}}
  • {{/repo}} Data Template
  • resque
  • hub
  • rip
  • Output

    Slide 22

    Slide 22 text

    Fragments base.mustache:

    Presentations

    {{#presentations}} {{> presentation}} {{/presentations}} ! presentation.mustache:

    {{title}}

    {{startTime}}

    Template

    Slide 23

    Slide 23 text

    Jade

    Slide 24

    Slide 24 text

    Jade • Available at http://jade-lang.com • Node.js Template engine • Java Implementation: Jade4j • Compact notation

    Slide 25

    Slide 25 text

    Syntax

    Slide 26

    Slide 26 text

    Result

    Slide 27

    Slide 27 text

    Variables & Iterations • Variables ! • Iterations

    Slide 28

    Slide 28 text

    Fragments

    Slide 29

    Slide 29 text

    No content

    Slide 30

    Slide 30 text

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

    Slide 31

    Slide 31 text

    Template languages • SSP (Scala Server Pages) • SCAML (based on Ruby’s HAML) • Mustache • Jade

    Slide 32

    Slide 32 text

    Scaml • Based on HAML (HTML abstraction markup language) • Integrates Scala code • Well formatted markup • Similar to Jade

    Slide 33

    Slide 33 text

    Variables & Loops ! ! • Fragment support is possible • Inline Scala code

    Slide 34

    Slide 34 text

    Benchmark project

    Slide 35

    Slide 35 text

    Benchmark project • Simple Spring MVC application • Multiple view resolvers • Page that show a list of presentations • Uses page layouts / fragments • Uses i18n where possible

    Slide 36

    Slide 36 text

    Spring MVC

    Slide 37

    Slide 37 text

    public class Presentation { private Long id; private String title; private String speakerName; private String summary; private String room; private Date startTime; private Date endTime; }

    Slide 38

    Slide 38 text

    No content

    Slide 39

    Slide 39 text

    Project available • Project will be available as open source on github. • https://github.com/jreijn/spring-comparing- template-engines

    Slide 40

    Slide 40 text

    Results!

    Slide 41

    Slide 41 text

    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

    Slide 42

    Slide 42 text

    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!

    Slide 43

    Slide 43 text

    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

    Slide 44

    Slide 44 text

    Thank you! Questions? @jreijn [email protected]