Jump to: navigation, search


428 bytes added, 14:14, 3 April 2009
Extending your Html objects
=== Extending your Html objects ===
The Html class overloads the typical list methods append(), extend(), "+" and "+=" to enable you to easily build up your page. All of the following examples are valid uses of Html objects . Try them in an interactive Python session and use <object>.write(although they will not necessarily produce properly structured HTML)to view the results:
page foo = Html(indent=False) bar = Html('head') page foobar = foo + bar bar += Html('headmeta') page bar.extend('meta',bob="your uncle") bar.append('useless text in header') opa = Html('body') page foobar += opa opa = page opa + Html('div', id="my div", class_="my class") page.append("a random string) page opa += [ 'text beginning', Html('a','imbedded href',href=""), "text following" ] p = Html('p')+ ['the quality of mercy is not strained','it falleth as the gentle dew from heaven'] '''Note:''' The Html class does not implement list operations by index (e.g. list[1]) or slice (e.g. list[1:2]). Use these at your own risk as they may cause unpredictable results.
Of course, if you like you can add preformatted tags:
page += '&lt;ul&gt;&lt;li&gt;one&lt;/li&gt;&lt;li&gt;two&lt;/li&gt;&lt;/ul&gt;'
since the object derives from list, but then you are responsible for properly closing your tags.
ButHowever, if the tag argument begins with a "<", the constructor assumes that it is a preformatted tag:
page += Html('<title>My Title</title>')

Navigation menu