tryexceptpass / sofi

an OS agnostic UI module for Python
MIT License
387 stars 49 forks source link

fix: Added a shortcut for generating tag attributes (#73) #74

Closed Michael-F-Bryan closed 8 years ago

Michael-F-Bryan commented 8 years ago
Michael-F-Bryan commented 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.

tryexceptpass commented 8 years ago

Looks good, few things I'm thinking about before pulling in though:

Michael-F-Bryan commented 8 years ago

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.

tryexceptpass commented 8 years ago

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.