Wim Deblauwe

Author
+ Follow
since Feb 21, 2024
Merit badge: grant badges
Biography
Software engineer. Writes mostly Java. Author of Taming Thymeleaf and Modern frontends with htmx.
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Wim Deblauwe

Congratulations to the winners!
10 months ago
Not really. You can read https://htmx.org/essays/when-to-use-hypermedia/#hypermedia-not-a-good-fit-if for some general guidelines.

Vaadin is a good alternative if your team is not interested in learning html and css and really wants to stay 100% in Java. But if you currently have frontend developers using html/css and JavaScript, then a templating engine that uses html and htmx for the interactivity is something they can learn. Doing everything in Java would probably scare them away too much
10 months ago
I don't know Flask or Django, so I don't know how to answer that.

In any case, you can get a long way already with htmx using any backend technology that allows to return html (snippets). There is an interesting post at https://htmx.org/essays/when-to-use-hypermedia/#hypermedia-not-a-good-fit-if that explains more when htmx might not be a good fit for your project.
10 months ago
Not so sure if I understand your question correctly.

htmx could be regarded as a fancy wrapper around doing AJAX calls, but it brings a lot of convenience also around how the returned HTML is swapped back into the current page.

Maybe you can elaborate your question a bit more?
10 months ago
Interesting question that maybe warrants its own topic on the forum here.

For a non-rolling rollout case, htmx is easier regarding versioning. The server just sends the updated HTML to the browser. There is no chance that the old JavaScript application cannot talk to the updated server.

Although, in all honesty, there are scenario's thinkable that your browser loads a page, and before htmx does a request to fetch an html snippet to swap, the server is updated and that call is either not possible anymore, or the returned html is not structured in a way that makes sense for the html that is already in the browser. In that case, the user will have to do a page refresh or you could always send a version number with all htmx requests and the server could check that. If it notices the version is different, it can force the browser to do a full-page refresh using the HX-Refresh header.

However, for rolling rollouts, I am not sure, I have never had to work with them.
10 months ago
Hi,

About HTML as data exchange format:

"Sending data as HTML, on the other hand, implies a tighter coupling between the backend and frontend, as the backend pre-defines the presentation format. "

In fact, the inverse is true. With a JSON format, your frontend needs to be built for the data that is sent as you are missing a lot of information in most JSON Data API's (usually called REST APIs). There is no information on the other endpoints you can call for example. So in most cases, there is some out-of-band knowledge needed on the (JavaScript) frontend to make everything work.

With HTML, the full interaction is part of the HTML. There can be buttons and links in the HTML that allow further interaction, without the need of the client to know this. It just renders the HTML and everything works. There is no need to update 2 applications in lock-step.

This also avoids problems around versioning the REST API. The server just returns the new "version"/html in case of server-side rendering and the browser renders it. With a Single Page Application, you need to make sure the frontend is updated to deal with the updated REST API.

regards,

Wim
10 months ago
Hi Maarten,

Like Frank and Luca mentioned, htmx does not favor any backend. If you would join their discord server, you will see they have channels for just about any backend technology you can think about (Java, Kotlin, C#, PHP, Python, Go, ...).

regards,

Wim
10 months ago
Hi,

I think that is not really true. With microservices, you usually have a backend-for-frontend (BFF) that orchestrates the calls to the other microservices and prepares the JSON in an easy-to-use format for the Single Page Application. If you do server-side rendering, the BFF itself could be the server-side renderer in a microservices architecture.

regards,

Wim
10 months ago
I understand that this post is 2 years old, so maybe not relevant anymore. But if I would do this today, I would use https://htmx.org/. That way, you can use Thymeleaf fragments and avoid a lot of the JavaScript.
Why do you think you can't use a `th:block` inside another? That should work fine.

I asked ChatGPT to translate it and it came up with this which seems very reasonable. I could not test it since I don't have your backend, but maybe it will help you along. It certainly _looks_ reasonable at first sight:



Feel free to ask a specific follow-up question if there is still something that is not working.
11 months ago
I wonder if the changes that you need to do can be done in CSS only. If that is the case, you could conditionally load different CSS files depending on a model attribute you set in the controller. That way, you can have 1 controller with just 2 methods that delegate to a single private method with the actual controller logic without duplication.

Something like this:



In your Thymeleaf template, use the `cssLink` variable to point to the CSS file.
11 months ago