shootout! template engines on the jvm
DESCRIPTION
TRANSCRIPT
![Page 1: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/1.jpg)
Shootout! Template engines on the JVM
Jeroen Reijn | @jreijn | #jfall13
![Page 2: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/2.jpg)
About me
• Architect @ Hippo
• Worked on web based platforms since 2001 with PHP, XSLT, JSP, Velocity and Freemarker
• Blogger at http://blog.jeroenreijn.com
![Page 3: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/3.jpg)
Session agenda
• General overview
• Interesting template engines
• Benchmark project
• Performance results
![Page 4: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/4.jpg)
Template engine?
• What is a Template ?
• What is a Template Engine ?
• How many Java Template Engines ?
• Java-Source.net : 21
• Wikipedia: 17
![Page 5: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/5.jpg)
Most commonly used
• JSP
• Freemarker
• Velocity
![Page 6: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/6.jpg)
Overview
![Page 7: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/7.jpg)
Selection criteria
• Project activity
• Framework support
• Active Community
• Brings added value
• IDE support
![Page 8: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/8.jpg)
Most interesting
• Thymeleaf
• Mustache
• Jade
• Scalate
![Page 9: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/9.jpg)
Thymeleaf
![Page 10: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/10.jpg)
Overview
• Available at http://www.thymeleaf.org/
• Java, DOM based template engine
• XML / XHTML / HTML5
• Full Spring MVC integration
• ‘Natural templating’
![Page 11: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/11.jpg)
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”
![Page 12: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/12.jpg)
JSP in browser
![Page 13: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/13.jpg)
Thymeleaf in browser
![Page 14: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/14.jpg)
![Page 15: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/15.jpg)
Variables & Loops
• Variables
• Loops
![Page 16: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/16.jpg)
Fragments
• Include fragment
• Define fragment based on th:fragment
![Page 17: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/17.jpg)
{{ Mustache }}
![Page 18: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/18.jpg)
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.
![Page 19: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/19.jpg)
Overview
• Java implementaions:
• JMustache
• Mustache.java
• Not web specific
• Spring MVC integration as an add-on
![Page 20: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/20.jpg)
Variables
{“greeting” : “Hello”,
“message” : “World”}
{{greeting}} {{message}}
Data Template
Hello World
Output
![Page 21: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/21.jpg)
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
![Page 22: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/22.jpg)
Fragments
base.mustache:<h2>Presentations</h2>{{#presentations}} {{> presentation}}{{/presentations}}
presentation.mustache:<h3>{{title}}</h3><p>{{startTime}}</p>
Template
![Page 23: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/23.jpg)
Jade
![Page 24: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/24.jpg)
Jade
• Available at http://jade-lang.com
• Node.js Template engine
• Java Implementation: Jade4j
• Compact notation
![Page 25: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/25.jpg)
Syntax
![Page 26: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/26.jpg)
Result
![Page 27: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/27.jpg)
Variables & Iterations
• Variables
• Iterations
![Page 28: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/28.jpg)
Fragments
![Page 29: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/29.jpg)
![Page 30: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/30.jpg)
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, ...
![Page 31: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/31.jpg)
Template languages
• SSP (Scala Server Pages)
• SCAML (based on Ruby’s HAML)
• Mustache
• Jade
![Page 32: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/32.jpg)
Scaml
• Based on HAML (HTML abstraction markup language)
• Integrates Scala code
• Well formatted markup
• Similar to Jade
![Page 33: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/33.jpg)
Variables & Loops
• Fragment support is possible
• Inline Scala code
![Page 34: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/34.jpg)
Benchmark project
![Page 35: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/35.jpg)
Benchmark project
• Simple Spring MVC application
• Multiple view resolvers
• Page that show a list of presentations
• Uses page layouts / fragments
• Uses i18n where possible
![Page 36: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/36.jpg)
Spring MVC
![Page 37: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/37.jpg)
public class Presentation { private Long id; private String title; private String speakerName; private String summary; private String room; private Date startTime; private Date endTime;}
![Page 38: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/38.jpg)
![Page 39: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/39.jpg)
Project available
• Project will be available as open source on github.
• https://github.com/jreijn/spring-comparing-template-engines
![Page 40: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/40.jpg)
Results!
![Page 41: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/41.jpg)
Performance
12,50s
16,00s
19,50s
23,00s
26,50s
30,00s
Total time taken for 25.000 requests (less is better)
Thymeleaf - v2.1.0Jade4j - v0.3.17Scalate - v1.6.1Mustache - v1.8Freemarker - v2.3.19Velocity - v1.7JSP - 2.1
![Page 42: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/42.jpg)
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
![Page 43: Shootout! template engines on the jvm](https://reader034.vdocuments.us/reader034/viewer/2022051609/54623f4aaf79597c138b4952/html5/thumbnails/43.jpg)
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