Usage with datastar #105
Replies: 2 comments 4 replies
-
|
Hmm there are also dots possible in the attribute names. Don't have a good answer for those. |
Beta Was this translation helpful? Give feedback.
-
|
Could you add a helper (with a short name) such as import htpy as h
def _datastar_attr(attr: str) -> str:
return "__".join([part.replace("_", "-") for part in attr.split("__")])
def ds(**kwargs: str) -> dict[str, str]:
return {"data-" + _datastar_attr(k): v for k, v in kwargs.items()}
print(h.div(ds(on_mouseover__once="@get('/something'")))output: (It was a short example, it does not deal with . exactly as you would probably like it to) I think datastar/AlpineJS/HTMX does interesting things with attributes. I do not think we should add support for those directly into htpy. There will always be new tools/attributes etc that use attributes in interesting ways and supporting them directly in htpy will be hard since many of them conflicts with each other. The "special case" for hyperscript is just to make it possible to use Also see #57 and my response there: #57 (reply in thread) |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
With Datastar, you add data-* attributes to your HTML tags to add various functionality. It also has 'modifiers' for some of these attributes that get added after two underscores in the attribute name. e.g.
data-on-mouseover__once. I guess the only way to handle this is to use the positional attrs dict rather than using kwargs, i.e.div({"data-on-mouseover__once": "@get('/something')})Just wanted to see if anyone had any good ideas to avoid that.I'm happy to special case it in my own apps, but I don't think we have a mechanism for that right now. Is there some way we could allow an user defined
_python_to_html_name? An easy way to subclassBaseElementto add this behavior but not have to redefine all the tags?I don't think datastar is popular enough (yet) to add special casing directly, but I see there is already something for hyperscript. Are there other common libraries that use a double -- in attribute names where leaving them as underscores would mess them up?
Beta Was this translation helpful? Give feedback.
All reactions