Convert underscore to hyphens in generic attributes names
Just a draft idea to address https://github.com/zostera/django-bootstrap5/issues/267 to see if there is any interesting in solving the issue
The change replace _ to - in generic tag attributes
So i can do something like {% bootstrap_button data_bs_toggle="collapse" data_bs_target="#update-group-9" button_type="reset" content="Cancel" %}
Generic attributes (like data_bs_toggle) are passed to render_tag without any changes while templatetag arguments (like button_type) are not, so it's safe to make this change without interfering django-bootstrap5 behaviour
Just a draft idea to address https://github.com/zostera/django-bootstrap5/issues/267 to see if there is any interesting in solving the issue
Yes, please merge these changes.
@dyve is this something you'd be interested to merge? If so I will complete this with tests to make it actually mergeable
The idea of converting data_ tot data- is interesting. For anything not starting with data_, it becomes a possible risk I think.
the idea behind this PR is to allow integrating htmx attributes (hx-*) which is currently not possible to support
Ok, Let's see it.
@dyve this is a first draft. I included tests to check that no existing attribute is altered
Came here to open an issue about this. Being able to add arbitrary attributes is almost a necessity these days, and it in the template seems less hacky than setting it on the python side.
Although this only seems to work for buttons, it would be great if we could pass through extra attributes for fields as well.
I'm interested in this solution as well. Right now I use a custom template tag and overwrite the django_bootstrap5 render_tag() function with the code changes from this PR. It works, but I would appreciate a native solution.
This looks pretty useful! With the data_ prefix, it should be mostly safe.
A possible addition is to support custom prefixes through a setting. That offers options to:
- (default)
BOOTSTRAP5["hyphen-attributes-prefixes"] = ["data"], converts alldata_*todata-* -
BOOTSTRAP5["hyphen-attributes-prefixes"] = ["data", "hx"], same as 1., and convertshx_*tohx-* -
BOOTSTRAP5["hyphen-attributes-prefixes"] = []No conversion.
It should be straightforward to maintain, and leaves an option for users wanting custom prefixes.
https://github.com/zostera/django-bootstrap5/pull/524#issuecomment-2485984608
I’ll try to push an update for this in the coming weeks.
I'm actually trying to figure it out right now, watch this space.
See #737
@yakky I continued this (with a parameter added) in #737. I'll make sure you get the credits if we merge this. If there is a better way on GitHub to continue work on a branch in someone else's repo, please let me know.
Closing because this will be fixed in #737