Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Static Private Attributes Class Reference
Inheritance diagram for
Inheritance graph
Collaboration diagram for
Collaboration graph

List of all members.

Public Member Functions

def __init__
def __call__
def append
def serialize
def __call__
def __unicode__
def __str__
def __add__

Public Attributes


Static Private Attributes

list __slots__ = ['tagname', 'attr']

Detailed Description

Simple XHTML output generator based on the builder pattern.

Construct XHTML elements by passing the tag name to the constructor:

>>> print Element('strong')

Attributes can be specified using keyword arguments. The values of the
arguments will be converted to strings and any special XML characters

>>> print Element('textarea', rows=10, cols=60)
<textarea rows="10" cols="60"></textarea>
>>> print Element('span', title='1 < 2')
<span title="1 &lt; 2"></span>
>>> print Element('span', title='"baz"')
<span title="&#34;baz&#34;"></span>

The " character is escaped using a numerical entity.
The order in which attributes are rendered is undefined.

If an attribute value evaluates to `None`, that attribute is not included
in the output:

>>> print Element('a', name=None)

Attribute names that conflict with Python keywords can be specified by
appending an underscore:

>>> print Element('div', class_='warning')
<div class="warning"></div>

While the tag names and attributes are not restricted to the XHTML language,
some HTML characteristics such as boolean (minimized) attributes and empty
elements get special treatment.

For compatibility with HTML user agents, some XHTML elements need to be
closed using a separate closing tag even if they are empty. For this, the
close tag is only ommitted for a small set of elements which are known be
be safe for use as empty elements:

>>> print Element('br')
<br />

Trying to add nested elements to such an element will cause an

>>> Element('br')('Oops')
Traceback (most recent call last):
AssertionError: 'br' elements must not have content

Furthermore, boolean attributes such as "selected" or "checked" are omitted
if the value evaluates to `False`. Otherwise, the name of the attribute is
used for the value:

>>> print Element('option', value=0, selected=False)
<option value="0"></option>
>>> print Element('option', selected='yeah')
<option selected="selected"></option>

Nested elements can be added to an element by calling the instance using
positional arguments. The same technique can also be used for adding
attributes using keyword arguments, as one would do in the constructor:

>>> print Element('ul')(Element('li'), Element('li'))
>>> print Element('a')('Label')
>>> print Element('a')('Label', href="target")
<a href="target">Label</a>

Text nodes can be nested in an element by adding strings instead of
elements. Any special characters in the strings are escaped automatically:

>>> print Element('em')('Hello world')
<em>Hello world</em>
>>> print Element('em')(42)
>>> print Element('em')('1 < 2')
<em>1 &lt; 2</em>

This technique also allows mixed content:

>>> print Element('p')('Hello ', Element('b')('world'))
<p>Hello <b>world</b></p>

Elements can also be combined with other elements or strings using the
addition operator, which results in a `Fragment` object that contains the

>>> print Element('br') + 'some text' + Element('br')
<br />some text<br />

Definition at line 331 of file

Constructor & Destructor Documentation

def (   self,
  tagname_ = None,

Definition at line 430 of file

00431     def __init__(self, tagname_=None, **attr):
00432         Fragment.__init__(self)
00433         if tagname_:
00434             self.tagname = tagname_
00435         self.attr = {}
00436         self(**attr)

Member Function Documentation

def (   self,
) [inherited]

Definition at line 327 of file

00328     def __add__(self, other):
00329         return Fragment()(self, other)

def (   self,
) [inherited]

Definition at line 308 of file

00309     def __call__(self, *args):
00310         for arg in args:
00311             self.append(arg)
00312         return self

Here is the call graph for this function:

def (   self,

Definition at line 437 of file

00438     def __call__(self, *args, **attr):
00439         self.attr.update(attr)
00440         return Fragment.__call__(self, *args)

Definition at line 324 of file

00325     def __str__(self):
00326         return ''.join(self.serialize())

Here is the call graph for this function:

Definition at line 321 of file

00322     def __unicode__(self):
00323         return u''.join(self.serialize())

Here is the call graph for this function:

def (   self,
Append an element or string as child node.

Reimplemented from

Definition at line 441 of file

00442     def append(self, node):
00443         """Append an element or string as child node."""
00444         assert self.tagname not in _EMPTY_TAGS, \
00445             "'%s' elements must not have content" % self.tagname
00446         Fragment.append(self, node)

Here is the caller graph for this function:

Generator that yield tags and text nodes as strings.

Reimplemented from

Definition at line 447 of file

00448     def serialize(self):
00449         """Generator that yield tags and text nodes as strings."""
00450         starttag = ['<', self.tagname]
00451         for name, value in self.attr.items():
00452             if value is None:
00453                 continue
00454             if name in _BOOLEAN_ATTRS:
00455                 if not value:
00456                     continue
00457                 value = name
00458             else:
00459                 name = name.rstrip('_').replace('_', '-')
00460             starttag.append(' %s="%s"' % (name.lower(), escape(value)))
00462         if self.children or self.tagname not in _EMPTY_TAGS:
00463             starttag.append('>')
00464             yield Markup(''.join(starttag))
00465             for part in Fragment.serialize(self):
00466                 yield part
00467             yield Markup('</%s>', self.tagname)
00469         else:
00470             starttag.append(' />')
00471             yield Markup(''.join(starttag))

Here is the caller graph for this function:

Member Data Documentation

Reimplemented from

Definition at line 428 of file

Definition at line 434 of file

Definition at line 291 of file

Definition at line 433 of file

The documentation for this class was generated from the following file: