Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Thymeleaf include and replace?

Tags:

thymeleaf

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

like image 774
codingbash Avatar asked May 08 '16 19:05

codingbash


People also ask

How do I create a fragment in Thymeleaf?

To define a Thymeleaf fragment, you need to use the th:fragment attribute. You can define fragments either in separate files (recommended) or in a common file. It is also a good practice to place all fragments in a dedicated folder called fragments inside the templates directory ( src/main/resources/templates/ ).


1 Answers

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).

like image 124
Iwo Kucharski Avatar answered Sep 28 '22 04:09

Iwo Kucharski