Thymleaf Templates.

I have been trying to use a base template for most of my pages for more consistent styling and to allow for multiple design changes to be possible globally without having to add 1 new thing to each page. essentially my goal is to have a page and let the content page extend it. meaning it only changes barely anything instead of pulling each thing in separately. html will be attached in following messages.
No description
30 Replies
JavaBot
JavaBot2w ago
This post has been reserved for your question.
Hey @Hype_the_Time! 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.
Hype_the_Time (PING ON REPLY)
content page (leaderboard.html) template (template/default.html) result
Hype_the_Time (PING ON REPLY)
(thing its doing right now is taking the leadbaord html and overwriting its content tag with the content tag from leaderboard)
JavaBot
JavaBot2w 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.
dan1st
dan1st2w ago
you can use thymeleaf fragments so essentially you can create a default page and include the other pages in it or create normal pages and include things like the footer
Hype_the_Time (PING ON REPLY)
I tried. Have a look at the HTML's I attached. My 0roblem is that it's taking it from the default instead of it being the only thing not from default.
dan1st
dan1st2w ago
?
Hype_the_Time (PING ON REPLY)
Instead of pulling things in individually I would like to use extending instead.
dan1st
dan1st2w ago
I think you can pass a parameter that decides which fragment you want to include
dan1st
dan1st2w ago
idk what you mean with extending yeah viewing html files is really annoying on mobile
Hype_the_Time (PING ON REPLY)
Basically. You have default.HTML in templates. You define the basic layouts there such as that utf 8 is used, the basic scripts, the default styles,... Meaning the only thing you want to change in each page essentially is its content. If you want to add something across all pages for example a element informing about a change instead adding a pull on each page you would only update default.HTML.
dan1st
dan1st2w ago
So in your controller, you always load the default page but you add an attribute for the actual page you are interested/want to replace, e.g. model.addAttribute("content", "leaderboard"); And then in your template:
<div th:replace="~{${content} :: content}">
<div th:replace="~{${content} :: content}">
idk about this being the correct syntax but I mean replacing the elements within the template
Hype_the_Time (PING ON REPLY)
Defining the content in the code doesn't seem the most optimal either. I guessed that it was a solution. But it would blow up the code size.
dan1st
dan1st2w ago
Instead of return "leaderboard";, you could do return page("leaderboard") and the page method sets stuff up for it (you could use a static import or whatever for that method)
Hype_the_Time (PING ON REPLY)
Just thought of this but the general approach with model attribute setting means you can't preview the pages either.
dan1st
dan1st2w ago
preview?
Hype_the_Time (PING ON REPLY)
Telling intelij hey open this in a browser so you can look the the content etc.
dan1st
dan1st2w ago
I mean, you can just start the application and configure it to live reload stuff
Hype_the_Time (PING ON REPLY)
Also no HTML highlighting
dan1st
dan1st2w ago
What? Why does that not work? Both the template and the file with the fragments are normal html files
Hype_the_Time (PING ON REPLY)
If it's defined in Java Strings?
dan1st
dan1st2w ago
what is defined as Java strings?
Hype_the_Time (PING ON REPLY)
I thought that's what you meant with the attribute setting essentially. How would you do it in HTML files?
dan1st
dan1st2w ago
the only thing you specify as a String is which page is selected
Hype_the_Time (PING ON REPLY)
But how do you overwrite only that specific part?
dan1st
dan1st2w ago
If you want to load leaderboard.html, you do
model.addAttribute("content", "leaderboard");
return "template";
model.addAttribute("content", "leaderboard");
return "template";
and then something like that (I didn't test it, idk the correct syntax)
JavaBot
JavaBot2w 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.

Did you find this page helpful?