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:
- SparkJava: —A micro framework for creating web applications in Java 8 with minimal effort
- SparkJava: Authentication—login/logout, and securing various pages in your app
- SparkJava: Bootstrap—Adding a nicer looking UI, with common navigation, drop down menus, etc.
- SparkJava: Facebook API—Authenticate with Facebook, then access the Facebook API
- SparkJava: Getting Started—A more clear tutorial
- SparkJava: Github API—Authenticate with Github, then access the Github API
- SparkJava: Image Files—and any other static files your application needs
- SparkJava: MongoDB—Using the NoSQL database MongoDB with SparkJava
- SparkJava: pac4j—Securing a SparkJava webapp, with options for OAuth or LDAP
- SparkJava: RESTful APIs—Creating RESTful APIs with SparkJava
- SparkJava: SLF4J—What is the Simple Logging Framework For Java, and how to configure it
- SparkJava: Templates—The various template engines you can use with SparkJava