recursive.codes

recursive.codes

recursive.codes


The Personal Blog of Todd Sharp

Spark Java Views Using Thymeleaf - Layouts

Posted By: Todd Sharp on 4/5/2017 5:30 GMT
Tagged: Groovy, Java, Spark Java, Thymeleaf

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.

To illustrate, let's create three separate fragments, one called head.html, one called nav.html and the final one called foot.html.  I've saved these in /src/main/groovy/resources/templates/fragments.  To make it more realistic, I've dropped in Bootstrap since that's what I'd usually do.  Here is the simple code for each:

Now back in our Bootstrap class, in the main() method, I've created a Groovy closure to grab any 'common' model bits:

Then I modified the route for /thymeleaf to include the commonModel in the model I'm using to render that view:

And now it's just a matter of using the th:replace attribute in my view wherever I'd like the fragments rendered:

Note that since my fragments were not in the /templates directory, but a subdirectory called 'fragments', I had to pass the path from /templates in the th:replace attribute.  Also notice that you can pass model variables to your fragment as I did on line 10.  This is crucial for any layout system as layout bits are rarely purely static.  

Compared to Sitemesh (which is what I'm used to in Grails) I think I'm a fan of the way Thymeleaf handles this.  Something about Sitemesh always felt a little "backwards" (the layout includes the view) so I'm happy to see Thymeleaf approach it in a more "forward" manner (the view includes the layout bits).

Image by SplitShire from Pixabay



Related Posts

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

Querying Autonomous Database from an Oracle Function (The Quick, Easy & Completely Secure Way)

I've written many blog posts about connecting to an Autonomous DB instance in the past. Best practices evolve as tools, services, and frameworks become...

Sending Email With OCI Email Delivery From Micronaut

Sending Email With OCI Email Delivery From Micronaut

Email delivery is a critical function of most web applications in the world today. I've managed an email server in the past - and trust me - it's not fun...

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

Brain to the Cloud - Part III - Examining the Relationship Between Brain Activity and Video Game Performance

In my last post, we looked at the technical aspects of my Brain to the Cloud project including much of the code that was used to collect and analyze the...

Note: Comments are currently closed on this blog. Disqus is simply too bloated to justify its use with the low volume of comments on this blog. Please visit my contact page if you have something to say!