Currently I have created a fragment which takes a list of properties and urls in order to create a menu:
menuFrag (thymeleafMap)
I wanted to populate this in the front end but I am not sure how to make directly in thyme leaf
<div th:replace="fragments/menu :: menuFrag ((<#{menu.item1},@(/menuItem1)><(#{menu.item2},@(/menuItem2)>))"></div>
Is there a way to do this or would it I have to pass this information down from the controller?
Also if there was a way for me to just pass in two array's that would also work.
It is actually possible to write map literals in thymeleaf, like this:
<div th:replace="fragments/menu :: menuFrag (menu=${ {{item:'Item1',url:'/menuItem1'},{item:'Item2',url:'/menuItem2'}} })"></div>
(Note that it is in fact a map nested into a list to be able to iterate over the outer one. Thymeleaf (or should I say SpringEL) seems to produce LinkedHashMap for the map and ArrayList for the list according to my observations)
You can then iterate over the items in your menu fragment like this:
<ul th:each="item: $(menu)">
<li><a th:text="${item.item}" th:href="@{${item.url}}" />Item</li>
</ul>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With