Changes between Version 1 and Version 2 of MarkupBuilder
- Timestamp:
- Jul 8, 2006, 11:19:11 PM (18 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
MarkupBuilder
v1 v2 1 1 = Generating Markup Programmatically = 2 2 3 Markup provides a `builder`module which lets you generate markup from Python code using a very simple syntax.3 Markup provides a [wiki:ApiDocs/MarkupBuilder builder] module which lets you generate markup from Python code using a very simple syntax. 4 4 5 The main entry point to the `builder` module is the `tag` object (which actually isan instance of the `ElementFactory` class). Elements can be created through this `tag` object using attribute access, for example:5 The 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: 6 6 7 7 {{{ … … 13 13 }}} 14 14 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:15 This 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: 16 16 17 17 {{{ 18 18 #!python 19 >>> doc(tag.br) 20 <Element "p"> 19 21 >>> 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> 21 23 }}} 22 24 23 This is basically a shortcut for converting the `Element` to a [wiki:MarkupStream stream] and rendering the stream: 25 If 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 35 As 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: 24 36 25 37 {{{ … … 29 41 <markup.core.Stream object at 0x72d230> 30 42 >>> 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> 32 44 }}} 33 45