Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite HTML Functionality #111

Closed
grantjenks opened this issue Aug 30, 2021 · 3 comments
Closed

Rewrite HTML Functionality #111

grantjenks opened this issue Aug 30, 2021 · 3 comments

Comments

@grantjenks
Copy link

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
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants