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

1 messages · Page 1 of 1 (latest)

gusty knot
#

Title is self explanatory

chrome cloakBOT
#

This post has been reserved for your question.

Hey @gusty knot! 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.

quick flare
gusty knot
#

and then changing the content like i want to

quick flare
#

yes

gusty knot
#

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

quick flare
#

well the content could be in every page

#

since that's what differs between pages

#

so idk whether you want that

gusty knot
#

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

quick flare
#

as I said, idk whether a content fragment makes sense

#

but that's for you to decide

gusty knot
#

doesnt it make sense to change that

#

?

quick flare
#

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

gusty knot
#

that good

#

i can then include header, footer and nav menu

#

and just change content

quick flare
#

yeah

gusty knot
# quick flare yeah

<link rel="stylesheet" href="header.css">
<header>
<h1>Programme annuel des etudiants</h1>
</header>

this is my header.html

quick flare
#

Well you'd have one HTML where you have a block that you want to insert

gusty knot
#

?

quick flare
#

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>

and then you can do something like

<th:block th:replace="your/html/file/containing/the/header.html :: header" />
gusty knot
#

does it mean i can have oen big general.html

#

with general stuff

#

and then access different stuff

#

header

quick flare
gusty knot
#

navmenu

#

etc

quick flare
#

you can have one HTML with multiple fragments

gusty knot
#

yeah so I can have one general.html with multiple fragments

quick flare
#

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

gusty knot
#

nah rather do th:block

#

pointless to have useless divs

quick flare
#

yeah if you want everything to be in a div, you could do that if you want to

quick flare
#

depends

#

Like if you have a navigation, the whole thing could be in a <nav> or <div> anyways

gusty knot
#

th:replace="~{header.html :: header}"

#

this is the right syntax btw

#

they changed it

gusty knot
quick flare
#

I remember that being changed and updating stuff for it

quick flare