cenkalti / pyhtml

HTML generation library for Python
Other
72 stars 10 forks source link

ideas for improvement #13

Closed nihlaeth closed 7 years ago

nihlaeth commented 7 years ago

I'm writing a toolset for working with pyhtml. Things like functions for easily creating complicated bootstrap forms and such. While working on this I got several ideas that would make working with pyhtml easier. But I don't want to start working on a pull request if you don't agree with the direction I'm thinking in. So here are the changes I'd like to make:

  1. do _ conversion in attribute names before storing in Tag.attributes so you don't accidentally define things like data-target, id and class twice when working there directly
  2. make attributes accessible as properties (again doing the _ conversion thing)
  3. expose children via sequence methods on non-self-closing tags
nihlaeth commented 7 years ago

This would make code like this possible:

field = div(class_="default")(b("Hello"), i("there"))
if i_like_this_user:
    field.class_ = "beautiful"

for child in field:
    child.attributes["class"] = "green"

It's a bit of a silly example, but I hope you get my drift.

nihlaeth commented 7 years ago

The idea about conversion needs a bit of work, because this way you could still define both class and `class`. Maybe there should be a check at render time that throws an exception if an attribute is defined twice.

cenkalti commented 7 years ago

Hi @nihlaeth . Sorry for the delayed reply.

  1. do _ conversion in attribute names before storing in Tag.attributes so you don't accidentally define things like data-target, id and class twice when working there directly

My preference would be documenting this behavior and leave as it is. I guess it is easier to understand the current behavior.

  1. make attributes accessible as properties (again doing the _ conversion thing)

I guess it is better to have only one way for accessing properties. They are available at Tag.attributes. Zen of Python :)

  1. expose children via sequence methods on non-self-closing tags

Same as 2. Children are available at Tag.children.

nihlaeth commented 7 years ago

Thanks for considering it :)