Spark Java Views Using Thymeleaf - Layouts
In the last post we looked at plugging in Thymeleaf into a Spark Java application for view rendering. The concept was pretty simple: using the Thymeleaf engine, render a view with a map of variables to use as the model. But in reality, our applications need a bit more complexity. They need reusable layouts. In this post we’ll take a look at how to handle that with Thymeleaf.
Reusable layouts include things like headers, footers, scripts and other things like nav menus that are common across the application. Thymeleaf accommodates these by using what they call “fragments” - reusable blocks of code defined by the th:fragment attribute that can be called from your templates using the th:replace, th:insert or th:include attributes. You can read all about it in their docs, but let’s take a look at a practical example below.







\


