question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Difference in th:replace vs th:insert when using variables

See original GitHub issue

Suppose 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:closed
  • Created 2 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
wimdeblauwecommented, Sep 15, 2021
0reactions
ultraqcommented, Sep 23, 2021

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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found