thymeleaf: improving your spring view layer with natural templates

51
© 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission. Thymeleaf Improving your Spring View Layer with Natural Templates Emanuel Rabina - @u1traq

Upload: spring-io

Post on 18-Nov-2014

6.157 views

Category:

Technology


9 download

DESCRIPTION

Speaker: Emanuel Rabina With the disconnect between the languages of the web (HTML, CSS, Javascript) and the languages of the server (Java, Groovy, Scala, etc), many libraries and frameworks have been invented over the years to fill this void, often resulting in views filled with back-end code, views filled with specialized syntaxes, or even the invention of completely new view languages abstractions; all for the purpose of transforming our server-side ideas into HTML, and few of which actually look like the HTML that it ends up as. Enter Thymeleaf - a templating framework that uses HTML to create good old HTML. In this presentation, you'll be introduced to Thymeleaf, some of its features, how you can use it in your Spring web projects, the growing ecosystem being developed around it, and how it uses natural templates to keep the web designer on your team, and inside each and every one of us, happy.

TRANSCRIPT

Page 1: Thymeleaf: improving your Spring view layer with natural templates

© 2013 SpringOne 2GX. All rights reserved. Do not distribute without permission.

ThymeleafImproving your Spring View Layer with Natural Templates

Emanuel Rabina - @u1traq

Page 2: Thymeleaf: improving your Spring view layer with natural templates

http://www.thymeleaf.org/

@thymeleaf

Thymeleaf

Page 3: Thymeleaf: improving your Spring view layer with natural templates

Background

Page 4: Thymeleaf: improving your Spring view layer with natural templates

Hello World!<p> </p>

Page 5: Thymeleaf: improving your Spring view layer with natural templates

www.alanwood.net/unicode/utilities_editors.html

Page 6: Thymeleaf: improving your Spring view layer with natural templates

mozillaquest.com/stories_01/Composer-01/Moz_0_8_Composer01_story_02.html

Page 7: Thymeleaf: improving your Spring view layer with natural templates

<b> bold </b>

<i> </i>italic

<blink> </blink>...

Page 8: Thymeleaf: improving your Spring view layer with natural templates
Page 9: Thymeleaf: improving your Spring view layer with natural templates

Dynamic Web• Perl• JSP• PHP• ASP• GSP• anything-that-ends-with-P

Page 10: Thymeleaf: improving your Spring view layer with natural templates
Page 11: Thymeleaf: improving your Spring view layer with natural templates

Dynamic Web• Perl• JSP• PHP• ASP• GSP• anything-that-ends-with-P

• JSF• GWT

Page 12: Thymeleaf: improving your Spring view layer with natural templates

<h:panelGrid>

...

</h:panelGrid>

Page 13: Thymeleaf: improving your Spring view layer with natural templates

The Language Barrier

Page 14: Thymeleaf: improving your Spring view layer with natural templates

Client-side languages Server-side languages

HTML

CSS

JavaScript

Java

C#Ruby

PerlGroovy

Scala

?

Page 15: Thymeleaf: improving your Spring view layer with natural templates

sans-pants.com/2010/04/27/great-pants-imagery/

Page 16: Thymeleaf: improving your Spring view layer with natural templates

HTML

PHP

JSP

ASP.NET

Ruby on Rails

GSP

CGI

ScalaTemplates

CFM

Page 17: Thymeleaf: improving your Spring view layer with natural templates

www.flickr.com/photos/wgtncc/6834607270/

Page 18: Thymeleaf: improving your Spring view layer with natural templates

Enter Thymeleaf

Page 19: Thymeleaf: improving your Spring view layer with natural templates

Natural Templates

“The template can be a document as valid as the final result, the engine syntax doesn’t break the document structure.”

en.wikipedia.org/wiki/Comparison_of_web_template_engines

Page 20: Thymeleaf: improving your Spring view layer with natural templates

Natural Templates

Page 21: Thymeleaf: improving your Spring view layer with natural templates

(JSP example)

Page 22: Thymeleaf: improving your Spring view layer with natural templates
Page 23: Thymeleaf: improving your Spring view layer with natural templates

(Thymeleaf example)

Page 24: Thymeleaf: improving your Spring view layer with natural templates

Natural Templates

JSP opened w/ browser Thymeleaf opened w/ browser

Page 25: Thymeleaf: improving your Spring view layer with natural templates

<% ... %>

<?php ... ?>

<%= ... %>

<g:each...>

<c:forEach...>

@for <cfloop...>

<ui:repeat...>

#foreach

Page 26: Thymeleaf: improving your Spring view layer with natural templates

th:attr

th:attrappend

th:attrprepend

th:case

th:classappend

th:each

th:fragment

th:object

th:if

th:inline

th:unless

th:include

Page 27: Thymeleaf: improving your Spring view layer with natural templates

Natural Templates• Quick prototyping• Design in the browser• Encouraged to add text and content• No need to start an app server• It’s HTML

Page 28: Thymeleaf: improving your Spring view layer with natural templates

Extending Thymeleaf

Page 29: Thymeleaf: improving your Spring view layer with natural templates

Dialects• Prefix• Processors• Expression objects

theach, text, utext, ...#dates, #arrays, ...

Page 30: Thymeleaf: improving your Spring view layer with natural templates

public class MyDialect extends AbstractDialect implements IExpressionEnhancingDialect {

public Map<String,Object> getAdditionalExpressionObjects(...);

public String getPrefix();

public Set<IProcessor> getProcessors();

}

Page 31: Thymeleaf: improving your Spring view layer with natural templates

public class MyProcessor extends AbstractAttrProcessor {

public MyProcessor(); // Constructor

public int getPrecedence();

public ProcessorResult processAttribute( Arguments arguments, Element element, String attributeName);

}

Page 32: Thymeleaf: improving your Spring view layer with natural templates

(Custom dialect example)

Page 33: Thymeleaf: improving your Spring view layer with natural templates

Working with Spring

Page 34: Thymeleaf: improving your Spring view layer with natural templates
Page 35: Thymeleaf: improving your Spring view layer with natural templates

(Spring example)

Page 36: Thymeleaf: improving your Spring view layer with natural templates

Ecosystem

Page 37: Thymeleaf: improving your Spring view layer with natural templates

Early 2012

Page 38: Thymeleaf: improving your Spring view layer with natural templates

Extras / ‘official’

•Conditional CommentsProcessing of all the kinds of Internet Explorer conditional comments

•Spring Security 3Adds attribute processors and utility objects to use Spring Security expressions

•Tiles 2Use Apache Tiles 2 to compose your templates

•Eclipse PluginContent assist (autocomplete/suggestions) support in Eclipse

Page 39: Thymeleaf: improving your Spring view layer with natural templates

Community

•ThymolIncludes other pages referenced with th:include when viewed statically

http://sourceforge.net/u/jjbenson/wiki/thymol/

•Dandelion DataTables

http://dandelion.github.io/datatables

Ridiculously customizable tables for the display of data

•Layout Dialect

http://github.com/ultraq/thymeleaf-layout-dialect

Inheritance-based page creation, like SiteMesh or JSF + Facelets

Page 40: Thymeleaf: improving your Spring view layer with natural templates

Community

And many more:•modules for the Play Framework•a JRebel plugin option to not cache in development mode•integration with Apache Shiro•client-side form validation using your JSR-303 Bean Validation annotations•runtime optimization of JS/CSS resources•creation of arbitrary data-* attributes•Thymeleaf + Spring MVC Maven archetype•...

Page 41: Thymeleaf: improving your Spring view layer with natural templates

Who’s using Thymeleaf?

http://scienceblogs.com/thoughtfulanimal/2010/05/24/dog-personalities/

Page 42: Thymeleaf: improving your Spring view layer with natural templates

http://www.broadleafcommerce.com/

Page 43: Thymeleaf: improving your Spring view layer with natural templates

http://www.onegini.com/

Page 44: Thymeleaf: improving your Spring view layer with natural templates

http://www.ppi.de/

Page 45: Thymeleaf: improving your Spring view layer with natural templates

http://www.sahibinden.com/

Page 46: Thymeleaf: improving your Spring view layer with natural templates

http://www.trabesoluciones.com/

Page 47: Thymeleaf: improving your Spring view layer with natural templates

http://www.yobidrive.com/

Page 48: Thymeleaf: improving your Spring view layer with natural templates

How companies have used Thymeleaf• Migrate from other technologies• Take advantage of a Spring-integrated technology (thus making

use of the Spring developer community)• Improve workflow / communication• Allow front-end specialists to really hack the view

Page 49: Thymeleaf: improving your Spring view layer with natural templates

The future of Thymeleaf

? ? ?

Page 50: Thymeleaf: improving your Spring view layer with natural templates

Future plans• Attoparser• data-* attribute processors• Template fragments as variables• Template-only comments! :)• ‘TEXT’ template mode• Spring/Global formatters• De-coupling processing instructions from templates

Page 51: Thymeleaf: improving your Spring view layer with natural templates

Visit our website: http://www.thymeleaf.org/Follow us on Twitter: @thymeleaf

Talk to us on Twitter: @springcentralFind session replays on YouTube: spring.io/video

Learn More. Stay Connected.