@@ -12,10 +12,7 @@ class Element(object):
1212
1313 def __init__ (self , content = None , ** kwargs ):
1414 self .contents = [content ]
15- self .attributes = ""
16- # create a string with kwargs
17- for key , value in kwargs .items ():
18- self .attributes += (' {}="{}",' .format (key , value ))
15+ self .attributes = kwargs
1916
2017 def append (self , new_content ):
2118 self .contents .append (new_content )
@@ -24,8 +21,10 @@ def render(self, out_file):
2421 # loop the content list
2522 # add tags to beginning / end
2623 open_tag = ["<{}" .format (self .tag )]
27- if len (self .attributes ) > 0 :
28- open_tag .append (self .attributes [:- 1 ])
24+ #if any kwargs, add them in to starting tag
25+ if self .attributes != {}:
26+ for k , v in self .attributes .items ():
27+ open_tag .append (' {}="{}"' .format (k , v ))
2928 open_tag .append (">\n " )
3029 out_file .write ("" .join (open_tag ))
3130
@@ -71,15 +70,24 @@ def render(self, out_file):
7170 # loop the content list
7271 # add tags to beginning / end
7372 open_tag = ["<{}" .format (self .tag )]
74- out_file .write ("" .join (open_tag ))
73+ if self .attributes != {}:
74+ for k , v in self .attributes .items ():
75+ open_tag .append (' {}="{}"' .format (k , v ))
7576 for content in self .contents :
7677 try :
7778 if content is not None :
78- content . render ( out_file )
79+ raise TypeError
7980 except AttributeError :
8081 out_file .write (content )
82+ out_file .write ("" .join (open_tag ))
8183 out_file .write (" />\n " )
84+
85+ def append (self , content ):
86+ raise TypeError
8287
8388class Hr (SelfClosingTag ):
8489 tag = "hr"
85- #Hr(width=400)
90+ #Hr(width=400)
91+
92+ class Br (SelfClosingTag ):
93+ tag = "br"
0 commit comments