How to include a thymeleaf template into another one, to include header, footer etc

Title is self explanatory -
30 Replies
JavaBot
JavaBot3w ago
This post has been reserved for your question.
Hey @userexit! Please use /close or the Close Post button above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically marked as dormant after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
dan1st
dan1st3w ago
you can use fragments for that
dan1st
dan1st3w ago
Baeldung
Working with Fragments in Thymeleaf | Baeldung
Learn how to create reusable view components with Thymeleaf fragments to simplify template management.
userexit
userexitOP3w ago
do u recommend having a page with header, content, footer and then changing the content like i want to
dan1st
dan1st3w ago
yes
userexit
userexitOP3w ago
ok so i make a main page with header, menu, content, footer and then when i want to return something else i can change automatically the content but load the same page
dan1st
dan1st3w ago
well the content could be in every page since that's what differs between pages so idk whether you want that
userexit
userexitOP3w ago
yeah so like i make a header, content, footer and i inject in the content div for every page different content is that what fragments are for
dan1st
dan1st3w ago
as I said, idk whether a content fragment makes sense but that's for you to decide
userexit
userexitOP3w ago
well if content changes between pages doesnt it make sense to change that ?
dan1st
dan1st3w ago
The goal of a fragment is if you have the same thing over and over, you can include that and don't have to write it over and over
userexit
userexitOP3w ago
oh damn ye that good i can then include header, footer and nav menu and just change content
dan1st
dan1st3w ago
yeah
userexit
userexitOP3w ago
<link rel="stylesheet" href="header.css"> <header> <h1>Programme annuel des etudiants</h1> </header> this is my header.html
dan1st
dan1st3w ago
Well you'd have one HTML where you have a block that you want to insert
userexit
userexitOP3w ago
will it include the css automatically ?
dan1st
dan1st3w ago
Essentially you can have a HTML that contains something like
<th:block th:fragment="header">
<link rel="stylesheet" href="header.css">
<header>
<h1>Programme annuel des etudiants</h1>
</header>
</th:block>
<th:block th:fragment="header">
<link rel="stylesheet" href="header.css">
<header>
<h1>Programme annuel des etudiants</h1>
</header>
</th:block>
and then you can do something like
<th:block th:replace="your/html/file/containing/the/header.html :: header" />
<th:block th:replace="your/html/file/containing/the/header.html :: header" />
userexit
userexitOP3w ago
after the :: u put header too does it mean i can have oen big general.html with general stuff and then access different stuff header
dan1st
dan1st3w ago
that's the name of the fragment
userexit
userexitOP3w ago
navmenu etc
dan1st
dan1st3w ago
you can have one HTML with multiple fragments
userexit
userexitOP3w ago
yeah so I can have one general.html with multiple fragments
dan1st
dan1st3w ago
yes btw <th:block> just means Thymeleaf will insert that as-is you can also use <div> or whatever and Thymeleaf will put it in a div
userexit
userexitOP3w ago
nah rather do th:block pointless to have useless divs
dan1st
dan1st3w ago
yeah if you want everything to be in a div, you could do that if you want to
userexit
userexitOP3w ago
kinda pointless imo
dan1st
dan1st3w ago
depends Like if you have a navigation, the whole thing could be in a <nav> or <div> anyways
userexit
userexitOP3w ago
th:replace="~{header.html :: header}" this is the right syntax btw they changed it well just put those tags in ur general file
dan1st
dan1st3w ago
yeah I think so I remember that being changed and updating stuff for it your decision
JavaBot
JavaBot3w ago
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived. If your question was not answered yet, feel free to re-open this post or create a new one. In case your post is not getting any attention, you can try to use /help ping. Warning: abusing this will result in moderative actions taken against you.
Want results from more Discord servers?
Add your server