Closed Michael-F-Bryan closed 8 years ago
I changed the example in #73 a bit so you are only given a sub-string that you can then add more complicated attributes to. For example:
class Footer(Element):
....
def __str__(self):
output = [ "<footer" ]
attributes = [
('ident', 'id'),
('cl', 'class'),
('style', 'style'),
]
sub_str = self._attrs_to_string(attributes)
output.append(sub_str)
if self.attrs:
for k in self.attrs.keys():
output.append(' ' + k + '="' + self.attrs[k] + '"')
output.append(">")
for child in self.children:
output.append(str(child))
output.append("</footer>")
return "".join(output)
@tryexceptpass, It doesn't eliminate all the repetition but at least now you only need to handle the special cases. I imagine that a bit of refactoring might even iron out most of those as well.
Looks good, few things I'm thinking about before pulling in though:
attributes=[('cl','class'), ('ident', 'id')]
.None
, or maybe something else that makes sense (though not True
or False
), it just adds the word to the string. For example: if element has cl="abc"
, ident="123"
, checked=None
, it would translate to id="123" class="abc" checked
.I like the idea of adding that default argument. I've added a couple lines and a test to make sure it works.
I don't think I've ever seen/used a tag like <div id="123" class="abc" checked>
before, but it was a fairly trivial thing to implement and test.
Pulling in... Thanks a lot for the help and suggestions.
On your point about the attributes by themselves, yes they seemed odd to me, but there are a few things in bootstrap that work that way.
test/element_test.py
file_element_attributes()
function