Five Thymeleaf Format Examples

The objective of Version 0.3.6 of NixMash Spring was to move all Post Management functions to Administration and add a number of editing improvements. Posts are listed in the Dandelion Table below, where Thymeleaf is doing some nifty formatting behind the scenes.

We’re going to look at five Thymeleaf format examples:

  1. Setting a CSS Class based on a Model property value
  2. Using multiple Thymeleaf #String() operations on a single field
  3. How to format a Java ZonedDateTime value in HTML
  4. One-Tag Iterations with Thymeleaf
  5. Simple Checkbox IsChecked settings based on a Model property value

The Thymeleaf HTML we discuss next is shown below. At [1a] we’re setting the first of two Local Variables. ${post.postDate} is a Java ZonedDateTime, but that’s not going to stop us from formatting it exactly how we want it. Look at [1b] where we use Thymeleaf’s #dates() library. Notice we’re setting our creationDate to an Instant(), which is the trick to formatting a Java ZonedDateTime.

[2a] and [2b] demonstrate performing multiple Thymeleaf functions on a single value. The original values are POST, SINGLEPHOTO_POST, LINK_SUMMARY, etc. With the two operations on displayType they become “Post”, “Singlephoto Post” and “Link Summary.” Cool.

[3] demonstrates setting a CSS Class based on a Model property value. If the Post is not published, its faded text color serves as a visual reminder.

[4] shows how easy it is to set a Checkbox IsChecked value based on a Model property value.

[5] is an example of a One-Tag iterator where the all post tags are displayed with a single line of HTML.

Source Code Notes for this Post

All source code discussed in this post can be found in my NixMash Spring GitHub repo and viewed online here.