SparkJava: Templates

The various template engines you can use with SparkJava

Almost every web framework has some kind of templating engine built into it.

A templating engine allows you to build a web page by iterating over a data structure. A template is a mix of HTML, with stuff that looks a little like code.

For example, here is a template written using the Handlebars template engine.

The stuff inside the curly braces are commands that control the generation of content. The curly brace characters look like the handlebars of a bicycle if you look at them sideways; that’s what gives Handlebars it’s name (same with Mustache, which is another template engine.)

What is both wonderful and awful about SparkJava is that it gives you a wide variety of choices for your templating engine.

The list is shown on this page: http://sparkjava.com/documentation#views-and-templates. The ones that are marked as “mature” are listed here. I encourage you to choose one of these.

Templating Engine Link to some documentation
Velocity Velocity User Guide
Freemarker Freemarker Getting Started
Mustache Baeldung tutorial for Mustache using Java
Handlebars Handlebars.java
Jade Jade Syntax
Thymeleaf Thymeleaf Tutorial

There are five others marked as “we know very little about this”; I encourage you not to use those.

Each template engine has its particular pros and cons. Eventually, we may settle on one. For now, as you explore various SparkJava tutorials, notice which of these template engines is being used.

Some examples of tutorials and full webapp examples that use various templating engines:

Tutorial Template Engine Github Repo
Library website Velocity https://github.com/tipsy/spark-basic-structure

Related topics: