Skip to content

Conversation

@alexhughson
Copy link

This PR adds the necessary support to render HTML for the Unpoly and HTMX libraries.

The biggest change is adding util.novalue which can be set as the attribute of a class to make it render in HTML without a value.

It also adds hx_ and up_ as prefixes for attributes to be converted from underscores to dashes.

@nikalexis
Copy link

Have a look also in this pull request.

#203

@Knio
Copy link
Owner

Knio commented Dec 31, 2024

In the past I have not accepted any 3rd-party integrations (and still don't wish to in the main library), but would like to find a middle ground. Please see #210 if you'd like to continue this PR

# A list of attribute prefixes which mean that underscores should be converted to dashes
# This allows attributes like data_username or hx_post become data-username and hx-post
# hx_ prefix is for HTMX and up_ prefix is for Unpoly
SPECIAL_PREFIX_LIST = ('data_', 'aria_', 'up_', 'hx_')
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggestion: move this to dominate.ATTR_PREFIXES and add a function to modify it:

ATTR_PREFIXES = ['data_', 'aria_']

def add_attr_prefix(*args: tuple[str]):
  ATTR_PREFIXES.extend(args)

Then, a user who wants to use htmx or unpoly does dominate.add_attr_prefix('hx_', 'up_').

This provides flexibility without the need to support a 3rd party library by name.

Global settings are often discouraged but, in this case, it makes sense IMO since you aren't always guaranteed to have a top level object to configure. The Python stdlib also has top-level global configs for this in a number of places where they make sense.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maye DASHED_ATTRS is the right name for this.

'''
return text(s, escape=False)

class novalue:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd recommend changing this to set = () so you can do:

tags.a('foo', up_instant=set)

IMO, there is no need for it to be a class and be instantiated every time.

Alternatively or additionally, the html spec refers to such attributes as boolean attributes. So, IMO, the attribute handling code could just look for is True and, if that's the case, set the attr name without the value.

tags.a('foo', up_instant=True)

Personally, I'd just go with supporting True, document it, and and not add the special sentinel value for simplicity sake.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realize boolean attrs are already supported.

@rsyring
Copy link

rsyring commented May 31, 2025

@Knio this is a great library, thanks for all the leg work you have done to get it to this point. I didn't realize until I started using it how much I've been longing to get away from Jinja templates and raw HTML.

but would like to find a middle ground. Please see #210 if you'd like to continue this PR

I add some comments to the PR that I think, with minor changes, would make it a good middle ground. Almost no additional burden on dominate from a support perspective but all the flexibility needed to support any library that uses custom attribute prefixes for functionality.

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

Successfully merging this pull request may close these issues.

4 participants