adamchainz / django-htmx

Extensions for using Django with htmx.
https://django-htmx.readthedocs.io/
MIT License
1.62k stars 140 forks source link

Rewrite HTML Functionality #111

Closed grantjenks closed 3 years ago

grantjenks commented 3 years ago

Python Version

No response

Django Version

No response

Package Version

No response

Description

This is more an idea than an issue. Sorry if there's a better place to share.

I recently added HTMX to a website that wasn't using it before. It was a fairly typical CRUD-like app and after about the third page, I realized the transformations I was making (e.g. adding hx-target and hx-select on a bunch of elements) were fairly mechanical. I then wrote a function as a middleware that made the transformations (mostly) automatically.

The middleware worked by parsing the HTML response, looking for form tags, and adding the hx-target/hx-select fields when an id attribute was present on the tag. I typically would replace the outer HTML to get a nice interactive result. Does that make sense? The overall goal was to leave the templates as-is and use middleware to automatically add the "hx-" tag attributes.

I know this pattern won't be universal but I wanted to share here and ask for your thoughts. If you survey your use of HTMX in Django templates, are the changes mostly mechanical? And could we automate that?

adamchainz commented 3 years ago

Hi!

This is more an idea than an issue. Sorry if there's a better place to share.

This is fine - I've been meaning to make a "Feature Request" issue type as well.

I know this pattern won't be universal but I wanted to share here and ask for your thoughts. If you survey your use of HTMX in Django templates, are the changes mostly mechanical? And could we automate that?

This pattern sounds fairly specific to your project. My use of htmx has been quite different.

I would also not be a fan of doing any post-processing, as this makes things harder to debug (templates no longer match the real HTML, no locality of behaviour). I would rather use something localized, like a template tag, or simply copy-pasting throughout the project.

Overall the goal of this package is to be like htmx itself: provide many atomic building blocks, rather than a framework for one particular way of doing things.

So thanks for the suggestion but it's a “no thanks”.

—Adam

grantjenks commented 3 years ago

Oooh, I like the idea of a template tag rather than middleware. That's a good one, thanks!

Just an aside for myself: I wonder if I could design the middleware so that it worked automatically on the Django admin. Might be a fun way to transform the admin into a SPA. Also would be good practice to see what challenges are involved with making something work more generically.

My use of htmx has been quite different.

I'm curious to learn more here. Love to trade tips about Django + HTMX.

adamchainz commented 3 years ago

I'm curious to learn more here. Love to trade tips about Django + HTMX.

I'll be writing about htmx on my blog soon :)