Difference between Thymeleaf include and replace?

codingbash picture codingbash · May 8, 2016 · Viewed 21.4k times · Source

What is the difference between the two Thymeleaf attributes: th:include and th:replace?

Answer

Iwo Kucharski picture Iwo Kucharski · May 8, 2016

According to documentation if you have this situation:

<div th:include="..."> content here </div>

fragment will be placed inside <div> tag.

However when you use replace:

<div th:replace="..."> content here </div>

then <div> will be replaced by content.

Thymeleaf can include parts of other pages as fragments (whereas JSP only includes complete pages) using th:include (will include the contents of the fragment into its host tag) or th:replace (will actually substitute the host tag by the fragment’s).