tipsy / j2html

Java to HTML generator. Enjoy typesafe HTML generation.
https://j2html.com/
Apache License 2.0
765 stars 136 forks source link

HTMLX Support #215

Closed TareqK closed 2 years ago

TareqK commented 2 years ago

https://htmx.org/ is a library that adds extra tags to HTML to create new behaviors and follows the Localization of Behavior pattern. It would be great if we can support these extended tags in J2html, which would allow using j2html to build entire component-based webapps.

Im more than happy to contribute the change, if its fine with the maintainers. Otherwise, is there a way to extends j2html to add more attributes on a tag? something like "xdiv", "ximg", etc, which would have the extra attributes?

sembler commented 2 years ago

I've heard good things about htmx. To start I'd look at the Tag::attr methods to see if those meet your needs. They'll allow you to add any attribute you could want, without needing to extend any existing classes.

If you're interested in adding logic behind a Tag you could extend the ContainerTag or EmptyTag classes for whatever you need. It is also possible to create your own classes that provide custom rendering by implementing DomContent and the render(HtmlBuilder<T> builder, Object model) method.

TareqK commented 2 years ago

Id love it if I can extend the base classes and make it part of j2html to boot

Edit : my original plan was to create new versions of each tag BTW, this would save me a lot of effort

sembler commented 2 years ago

I think it would need to be distributed as a separate Jar, and I'd expect there to be many changes under the hood as we progress towards version 2.0.

TareqK commented 2 years ago

I Dont mind doing it like that, by extending each base class. But should this be in this repo or another repo?

Edit: Looking at this code base, I can probably use some regex magic and create 2 versions of the library, one with the HTMX tags(which are available for all container tags) and one without. Would that work?

sembler commented 2 years ago

My initial impression from the htmx docs is that this is heavily focused on getting the correct content for the correct attributes. To that end I'd suggest starting by defining small utilities to do that instead of worrying about deep integration with existing classes. Like Attribute put = HX.put("/messages") so that you can the use the attribute in J2html div().attr(HX.put("/messages")) to get <div hx-put="/messages"></div>. As you expand on that support class you could add features towards the specific syntax that HTMX recognizes like hx-trigger="mouseenter once". If you end up still pursuing integration with J2html you'll have all of those utilities ready to use behind the scenes.

TareqK commented 2 years ago

I went ahead and did that https://github.com/TareqK/j2htmx

TareqK commented 2 years ago

Closing this, as i created j2htmx to extend for now

sembler commented 2 years ago

Thanks @TareqK ! This looks really nice. I like where you are going with your examples.