Difference in th:replace vs th:insert when using variables
See original GitHub issueSuppose todos
is a model attribute that has a List
of TodoItemDto
instances.
This works:
<ul class="todo-list" th:remove="all-but-first">
<li th:each="item : ${todos}" th:insert="~{fragments :: todoItem(item)}">
</li>
</ul>
Given fragments.html
contains this:
<li th:fragment="todoItem(item)">
<div class="view">
<label th:text="${item.title}">Taste JavaScript</label>
</div>
</li>
However, when using th:replace
instead of th:insert
like this:
<ul class="todo-list" th:remove="all-but-first">
<li th:each="item : ${todos}" th:replace="~{fragments :: todoItem(item)}">
</li>
</ul>
Then I get an error:
Property or field ‘title’ cannot be found on object of type ‘java.lang.String’ - maybe not public or not valid?
Any idea why this is?
What I find also strange is that it also does not work with th:insert
if I rename item
to myItem
when calling the fragment (without changing anything to the fragment):
<ul class="todo-list" th:remove="all-but-first">
<li th:each="myItem : ${todos}" th:insert="~{fragments :: todoItem(myItem)}">
</li>
</ul>
This gives the same error that title
cannot be found on object of type String
.
For my usecase, I would really like to use th:replace
to have cleaner HTML output if possible.
Issue Analytics
- State:
- Created 2 years ago
- Comments:8 (2 by maintainers)
Top Results From Across the Web
Working with Fragments in Thymeleaf - Baeldung
Learn how to create reusable view components with Thymeleaf fragments to simplify ... Each view shows a different fragment use scenario.
Read more >Thymeleaf iteration and fragments - Wim Deblauwe
With th:replace , it replaces the element that it is declared on. The reason for this is Attribute Precedence in Thymeleaf.
Read more >Tutorial: Using Thymeleaf
A Thymeleaf context is an object implementing the org.thymeleaf.context.IContext interface. Contexts should contain all the data required for an ...
Read more >Thymeleaf + spring dynamic replace - Stack Overflow
Although Joe Essey's solution is working as well i solved with following code: <div th:replace="@{'fragments/' + ${template}} ...
Read more >Chapter 33 Thymeleaf flow control and fragments
It deals with these templating techniques that Thymeleaf offers: ... This is how to pass a variable ... The difference between th:insert and...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I published a blog post about this now: https://www.wimdeblauwe.com/blog/2021/09/14/thymeleaf-iteration-and-fragments/
Nice 🙂 And yeah I got a bit stuck figuring out why what you had a few posts back didn’t work, but good to see you’ve figured it out!