Reputation: 1114
I want to create a string variable in thymeleaf by iterating through a loop and concatenating values into this string variable. Then i want to display this string in a <span>
element. What i want to achieve can be written in java as follows:
String forDisplay = "";
foreach (MyObject o : myObjectCollection) {
if (o.type == 1) { forDisplay += o.stringValue; }
}
Then in i want to put this in an html element like span. I know how to use:
<span th:each="o : ${objectCollection}" th:if="${o.type == 1}" th:text="${o.stringValue}"></span>
But this creates <span>
for each of the elements that satisfy the condition. I just want to build-up my string in a th
tag free section and then i just want to display my string in a single <span>
element.
Upvotes: 0
Views: 1623
Reputation: 987
Here is how I join numbers into string using ", " as delimiter
<span th:each="instrumentDescriptor, iterStat : ${instrument.instrumentDescriptors}" th:text="!${iterStat.last} ? ${instrumentDescriptor.instrumentVersion} + ', ': ${instrumentDescriptor.instrumentVersion}"></span>
Upvotes: 0
Reputation: 344
Ahmet, take a look at Expression Utility Objects for Strings, from Thymeleaf docs.
You have three methods for joining items:
${#strings.arrayJoin(namesArray,',')} // For Arrays
${#strings.listJoin(namesList,',')} // For Lists
${#strings.setJoin(namesSet,',')} // For Sets
These Utility Objects offers lots of cool methods for Aggregation, Calendars and etc.
Att
Upvotes: 0