Menu Item Components
Explore how to build reusable menu item components in Thymeleaf using fragments and parameters. Understand dynamic CSS class handling and SVG icon inline styling to create a maintainable, scalable codebase. This lesson guides you through improving code efficiency and readability for both desktop and mobile menus in your Spring Boot application.
We'll cover the following...
Our index.html is already looking a lot better, but we still have a lot of duplication in our fragments themselves.
In the menu on the left side, each menu item looks similar to this:
The structure of menu items
We can identify the following structure:
- The
<a>HTML tag identifies the item as a link that can be clicked to navigate to that part of the application. - The
hrefattribute indicates where to link to. - The
<svg>child element contains the relevant icon for the menu item. - The “
dashboard” text is shown to the user for the link. - The
classattribute styles the menu item
Note: There is a slight difference in the class list as the Dashboard item is styled to look
selected, while the others are not.
Creating a fragment
Let’s create a fragment. Start with a file sidebar-buttons.html to put our fragment in:
This gives us the basic structure to work with. We then declare the following two parameters to our fragment:
linktitle