Edit:
The following question is solved! It can be done using Includes and Fragments.
Sam, although your answer is satsfactory to solve part of my problems I still have an doubt.
This method replaces an element and all of its ancestors with those specified in the layout and preserves the content of the original element.
It works well for layouts, but how about replace the content of a fragment only?
Lets suppose I have the following scenario. I'm developing a library of bootstrap widgets. Some widgets, like navbar have internal containers and this containers will contain other elements. Ex.:
fragments/navbar.html
<!-- navbar -->
<div th:fragment="navbar" class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Awesome Thymeleaf Navbar</a>
<ul class="nav">
<!-- children goes here -->
</ul>
</div>
</div>
<div>
Inside index.html
This is my Navbar:
<div th:replace="fragments/navbar :: navbar">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</div>
And get the following output:
This is my Navbar:
<!-- navbar -->
<div th:fragment="navbar" class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Awesome Thymeleaf Navbar</a>
<ul class="nav">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div>
</div>
<div>
Do you understand the difference? They way you shown to me replaces the element and its ancestors, but preserves the content. The result is the whole document root being replaced.
In some situations I need to replace just the element being replace. I need to keep its parents and contents too.
As I said it's solved. Here goes an example of how to implement this:
layouts/navbar.html:
<!DOCTYPE html>
<html>
<body>
<div layout:fragment="navbar" class="navbar">
<div class="navbar-inner">
<a class="brand" href="#">Navbar</a>
<ul class="nav" layout:fragment="navbar-content">
<!-- children goes here -->
</ul>
</div>
</div>
</body>
</html>`
document.html
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Home</title>
</head>
<body>This is my Navbar:
<div layout:include="layouts/navbar :: navbar" th:remove="tag">
<ul layout:fragment="navbar-content">
<li class="active"><a href="#">Home</a></li>
<li><a href="#">Link</a></li>
<li><a href="#">Link</a></li>
</ul>
</div>
</body>
</html>