Edgewall Software

Changes between Version 1 and Version 2 of MarkupBuilder


Ignore:
Timestamp:
Jul 8, 2006, 11:19:11 PM (18 years ago)
Author:
cmlenz
Comment:

Minor updates

Legend:

Unmodified
Added
Removed
Modified
  • MarkupBuilder

    v1 v2  
    11= Generating Markup Programmatically =
    22
    3 Markup provides a `builder` module which lets you generate markup from Python code using a very simple syntax.
     3Markup provides a [wiki:ApiDocs/MarkupBuilder builder] module which lets you generate markup from Python code using a very simple syntax.
    44
    5 The main entry point to the `builder` module is the `tag` object (which actually is an instance of the `ElementFactory` class). Elements can be created through this `tag` object using attribute access, for example:
     5The main entry point to the `builder` module is the `tag` object (which is actually an instance of the `ElementFactory` class). Elements can be created through this `tag` object using attribute access, for example:
    66
    77{{{
     
    1313}}}
    1414
    15 This produces an `Element` instance which can be further modified to add child nodes and attributes. It can also be directly rendered to XML text:
     15This produces an `Element` instance which can be further modified to add child nodes and attributes. This is done by “calling” the element: positional arguments are added as child nodes (alternatively, the `append` method can be used for that purpose), whereas keywords arguments are added as attributes:
    1616
    1717{{{
    1818#!python
     19>>> doc(tag.br)
     20<Element "p">
    1921>>> print doc
    20 <p>Some text and <a href="http://example.org/">a link</a>.</p>
     22<p>Some text and <a href="http://example.org/">a link</a>.<br/></p>
    2123}}}
    2224
    23 This is basically a shortcut for converting the `Element` to a [wiki:MarkupStream stream] and rendering the stream:
     25If an attribute name collides with a Python keyword, simply append an underscore to the name:
     26
     27{{{
     28#!python
     29>>> doc(class_='intro')
     30<Element "p">
     31>>> print doc
     32<p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p>
     33}}}
     34
     35As shown above, an `Element` can easily be directly rendered to XML text by `print`ing it or using the Python `str()` function. This is basically a shortcut for converting the `Element` to a [wiki:MarkupStream stream] and serializing that stream:
    2436
    2537{{{
     
    2941<markup.core.Stream object at 0x72d230>
    3042>>> print stream
    31 <p>Some text and <a href="http://example.org/">a link</a>.</p>
     43<p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p>
    3244}}}
    3345