image

the sleepy snake

index :: html :: TagInstances

tags

The html module supports all tags and attributes as specified in http://www.htmlhelp.com/reference/html40/. All tag classes in the module use names with the first letter capitalzed. HTML --> Html, TABLE --> Table, B --> B. All tag attributes are handled and should be specified lowercase.


All tags are derrived from the following base classes:

HtmlBase base class for all tags
TagBase base class for all non-generic tags

Tag instances provide the following attributes:

attrsa dict containing attributes of the tag
childrenlist of emidiate child tags of the tag
namename of the tag (lowercase, readonly)
parentparent of the tag or None (readonly))

Tag instances provide the following class attributes (considered readonly):

can_containlist of TT_* tag type constants
has_endtagboolenan flag
supported_attrslist of attrs (lowercase) the tag supports by default
typetype of the tag, one of the TT_* tag type constants

Tag instances provide the following methods:

__init__(**attrs)constructor
__call__(*others)adds one or a list of tags as children to the tag
copy()copies the tag
path(path)queries the tag for a childtags
save(outpath=None, tidy=False)saves the tag
walk(report=False)recursive iterator over the the tag

There are two ways to construct HTML content. One is by using list methods:

from html import *

tr = Tr()
tr.children.extend([Td(), Td()])
t = Table()
t.children.append(tr)

The other is more layout centric and more readable, using call()

from html import *

t = (
    Table()
    (
        Tr()
        (
            Td(),
            Td()
        )    
    )
)

Notes:

  • everything you add that is not a tag or a generic element will be added as Text() to a tag

attributes

attributes may be specified at initialization time or by accessing a tags attrs dict. Attribute values must always be specified as strings.

a = A(href="foo/bar")
a = A(**{"href": "foo/bar"})
a = A()
a.attrs["href"] = "bar"
a.attrs = {"href": "foo/bar", "name": "baz"}

# as a convenience, because it is so commonly used, 
# class may be specified keyword clss (constructor only)
a = A(clss="foo")

# flag attributes are set by passing "" (empty string) as value
a.attrs["cant_think_of_one_right_now"] = ""



child tags

child tags of a tag are always stored as list

ul = Ul()
ul.children = [Li(), Li()]