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

Repeatedly calling attr() on same attribute doesn't accumulate values #150

Open
cpsievert opened this issue Jul 30, 2021 · 1 comment
Open

Comments

@cpsievert
Copy link
Contributor

cpsievert commented Jul 30, 2021

For example, I'd expect the following to generate <button class="bar baz">foo</button>:

from dominate.tags import *

b = button("foo")
with b:
  attr(className = "bar")
  attr(className = "baz")

print(b)
<button class="baz">foo</button>

Accumulating is nice for proving a "base component" with a set of "base classes" that an end user could add to, for example:

def myButton(label):
  b = button(label)
  with b:
    attr(className = "btn")
  return b

b2 = myButton("foo")
with b2:
  attr(className = "btn-primary")

print(b2)

Would ideally generate

<button class="btn btn-primary">foo</button>
@JustinBacher
Copy link

JustinBacher commented Feb 22, 2024

So technically your ideal/expected result is actually unexpected behavior. The documentation on the attr function literally says:

Set attributes on the current active tag context

So in essence what you're doing when you call attr(className = "bar") you're quite literally setting className (class) to "btn" then when you call it again with attr(className = "baz") it's literally setting className (class) to "baz".

What you'd really want is the addition of a function along the lines of append_attribute or something, which adds to a current attribute if it exists or falls back to just setting the attribute if it doesn't exist.

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