pelme / htpy

Generate HTML in Python
http://htpy.dev
MIT License
261 stars 11 forks source link

Empty Attributes and Dots in Attribute Names #29

Closed avila-gabriel closed 5 months ago

avila-gabriel commented 5 months ago

Issue: Lack of Support for Empty Attributes and Dots in Attribute Names in htpy

Description

I'm currently integrating the htpy library with the Datastar framework and have encountered issues with how htpy handles HTML attributes. Specifically, htpy does not support:

  1. Empty attributes - Attributes in HTML that do not have a value.
  2. Dots in attribute names - Attributes that include dots (.) in their names.

These features are crucial for working with Datastar as you can see below.

Examples

  1. Dots in attribute names:
    <div data-on-store-change.remote="value"></div>
  2. Empty attributes:
    <div data-scroll-into-view></div>

    Current Workaround

    I have tried passing attributes as a dictionary to bypass the issue with dots in attribute names, which works fine, although not as delicious as using a parameter. However, this approach does not address the problem with empty attributes, as there is currently no way to include them without assigning a value.

    Request

    I am seeking a way to enhance htpy's functionality to support both empty attributes and attributes with dots in their names. Any guidance or potential updates to address these issues would be highly appreciated as I'm currently unsure how to proceed without these capabilities. Thank you for looking into this.

StabbarN commented 5 months ago

Regarding empty attributes have you tried Boolean attributes?

>>> div(data_scroll_into_view=True)
<Element '<div data-scroll-into-view></div>'>
pelme commented 5 months ago

Thanks for opening the issue.

There are a lot of characters in addition to dots that are not valid as python parameters, using the dictionary style is the way to go to deal with it. We do replace with - since is not typically used in HTML while - is not supported in python.

Do you have any ideas for how it could be improved?

pelme commented 5 months ago

I updated the docs slightly in a2af06c04a6fc749f2ee524cd981d3c1b0641471 and 2b7c9541e6905f0aadb8b6ad3d6e3b15a5b48019, I hope that helps make it a bit clearer. Please reopen the issue and/or get back with suggestions on improvements!

avila-gabriel commented 5 months ago

Thank you guys for your help. The only thing I can think of would be using double underscores for dots, as its the only python accepted that html don't, but once in data-* attributes which are valid html I have something to share, I'll open a new issue for it #33