How to include a thymeleaf template into another one, to include header, footer etc
Title is self explanatory
-
30 Replies
⌛
This post has been reserved for your question.
Hey @userexit! Please useTIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here./close
or theClose 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.
you can use fragments for that
Baeldung
Working with Fragments in Thymeleaf | Baeldung
Learn how to create reusable view components with Thymeleaf fragments to simplify template management.
do u recommend having a page with header, content, footer
and then changing the content like i want to
yes
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
well the content could be in every page
since that's what differs between pages
so idk whether you want that
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
as I said, idk whether a content fragment makes sense
but that's for you to decide
well if content changes between pages
doesnt it make sense to change that
?
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
oh damn ye
that good
i can then include header, footer and nav menu
and just change content
yeah
<link rel="stylesheet" href="header.css">
<header>
<h1>Programme annuel des etudiants</h1>
</header>
this is my header.html
Well you'd have one HTML where you have a block that you want to insert
will it include the css automatically
?
Essentially you can have a HTML that contains something like
and then you can do something like
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
that's the name of the fragment
navmenu
etc
you can have one HTML with multiple fragments
yeah so I can have one general.html with multiple fragments
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
nah rather do th:block
pointless to have useless divs
yeah if you want everything to be in a
div
, you could do that if you want tokinda pointless imo
depends
Like if you have a navigation, the whole thing could be in a
<nav>
or <div>
anywaysth:replace="~{header.html :: header}"
this is the right syntax btw
they changed it
well just put those tags in ur general file
yeah I think so
I remember that being changed and updating stuff for it
your decision
💤
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.