This site uses cookies to enhance your browsing experience and deliver personalized content. By continuing to use this site, you consent to our use of cookies. COOKIE POLICY COOKIE POLICY
We’ve been developing a UI using Spring Thymeleaf (2.1.2) on our team. I’ve noticed a serious dearth of examples outside of specific questions on Stack Overflow, so I figured I’d throw my hat in the ring and offer a few tips and tricks we’ve picked up over the 10 or so months working with this templating system. This is part one of a series. In each exploration, we will examine a few different pieces of Thymeleaf that could make your life a little easier. This first one is a few tips and tricks that should help keep your templates a little more readable.
Make Function Calls Into Your POJOs
Sometimes we need a little more power behind our retrieval operations. When it’s too complicated to try and manage it inline or Thymeleaf’s expressions don’t cover your use case, you can always use a formatter method on your domain object and call that directly in the template.
Let’s say we have a list of people with addresses, and we want to format the second address field on its own line if it’s there. We could use a ternary operator for that in a big messy concatenation, but for the sake of readability, it’s easier to wrap it in a function and let the model serve up the formatted text.
This markup produces:
Use Ternary Operators for Unreliable Info
Sometimes you run across a case where a piece of data isn’t present yet in the state of your application. In some of these cases, clever use of the ternary operator can make for an inline data swap instead of having to use th:if or th:block.
Use RowStat to Grab the Index Th:Each
There’s a secondary argument available to th:each – rowStat. It allows you to access certain values around the collection you are iterating over.
Use Th:Field for Fun and Profit
If you don’t want to type the name of your input over and over again, use th:field. It turns this:
into this:
Use Th:Object for Cleaner Templates
For a specific block of code, you can use th:object to allow direct reference to an object’s members. Once we do that to our table, it looks like this:
Note the change in the notation from ‘$’ to ‘*’. This tells Thymeleaf to look up the tree for a parent object.
That’s all for today. Next time we’ll cover standard expression libraries, Java 8 time expressions, data types and integrating Thymeleaf service objects with your javascript.
When There’s Too Much to Fix: How Smart Prioritization Unlocks Revenue at Scale
Every operations team has a backlog. The question isn’t whether you can clear it — it’s whether you’re clearing it in the right order. For most teams, the honest answer is no. And that gap between the order work gets done, and the order it should get done is quietly costing organizations millions. The Volume Problem High-volume exception processing shows up across […]
Creating Reusable Code Templates to Reduce Client Project Startup Time
In consulting, one of the least visible but most expensive phases of a project is the beginning. Teams can spend days or weeks setting up repositories, agreeing on structure, wiring basic infrastructure, and solving problems that have already been solved many times before. Code templates are a practical way to reduce overhead while improving consistency. […]
Player Three Has Entered the Game: How AI Is Finally Bridging the Divide Between Design and Engineering
As AI begins to become more prominent in our day-to-day lives, I find myself in a unique position. As a practicing software engineer and UI/UX designer, I am genuinely happy to see the introduction of AI tools begin to take shape in our industry. But more importantly, I am happy to start seeing the effects it is having on what has historically been a pretty challenging relationship: the […]
The Disappearing Middle of Software Work: Why the Bookends – Strategy & Impact – Matter Most Now
Here’s a question nobody in enterprise software wants to sit with: what happens to the middle? Not the middle of the org chart. The middle of the work. The vast, expensive layer of effort that has defined enterprise software delivery for thirty years—translating what the business wants into working code. The requirements-to-implementation pipeline. The “build phase.” […]
Full distributed tracing and exception capture for any application — without writing a single line of instrumentation code. View the source code on GitHub → The Premise Observability is essential for understanding what’s happening inside your services, but instrumenting an application by hand — adding trace spans, logging calls, and metric counters throughout your codebase […]
Building a Consultant in the Trenches: How Playing Offensive Line Shaped My Consulting Career
People often ask me the same question when they find out that I played college football: “Do you miss it?” On the surface, it’s a bad question with an obvious answer. Yes. However, if I give myself a minute to sit with that question, the answer is more nuanced. Yes, I miss playing football, but […]