Mercurial > genshi > mirror
annotate doc/builder.txt @ 386:c66370dfc41b trunk
Unit test fixes for Python 2.3.
author | cmlenz |
---|---|
date | Mon, 11 Dec 2006 11:13:35 +0000 |
parents | 2682dabbcd04 |
children |
rev | line source |
---|---|
226 | 1 .. -*- mode: rst; encoding: utf-8 -*- |
2 | |
3 ================================== | |
4 Generating Markup Programmatically | |
5 ================================== | |
6 | |
230 | 7 Genshi provides a ``builder`` module which lets you generate markup from Python |
226 | 8 code using a very simple syntax. The main entry point to the ``builder`` module |
9 is the ``tag`` object (which is actually an instance of the ``ElementFactory`` | |
10 class). You should rarely (if ever) need to directly import and use any of the | |
11 other classes in the ``builder`` module. | |
12 | |
13 | |
14 .. contents:: Contents | |
15 :depth: 2 | |
16 .. sectnum:: | |
17 | |
18 | |
19 Creating Elements | |
20 ================= | |
21 | |
22 Elements can be created through the `tag` object using attribute access, for | |
23 example:: | |
24 | |
230 | 25 >>> from genshi.builder import tag |
226 | 26 >>> doc = tag.p('Some text and ', tag.a('a link', href='http://example.org/'), '.') |
27 >>> doc | |
28 <Element "p"> | |
29 | |
30 This produces an ``Element`` instance which can be further modified to add child | |
31 nodes and attributes. This is done by “calling” the element: positional | |
32 arguments are added as child nodes (alternatively, the ``append`` method can be | |
33 used for that purpose), whereas keywords arguments are added as attributes:: | |
34 | |
35 >>> doc(tag.br) | |
36 <Element "p"> | |
37 >>> print doc | |
38 <p>Some text and <a href="http://example.org/">a link</a>.<br/></p> | |
39 | |
40 If an attribute name collides with a Python keyword, simply append an underscore | |
41 to the name:: | |
42 | |
43 >>> doc(class_='intro') | |
44 <Element "p"> | |
45 >>> print doc | |
46 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p> | |
47 | |
48 As shown above, an ``Element`` can easily be directly rendered to XML text by | |
49 printing it or using the Python ``str()`` function. This is basically a | |
50 shortcut for converting the ``Element`` to a stream and serializing that | |
51 stream:: | |
52 | |
53 >>> stream = doc.generate() | |
54 >>> stream | |
382
2682dabbcd04
* Added documentation for the various stream event kinds.
cmlenz
parents:
235
diff
changeset
|
55 <genshi.core.Stream object at ...> |
226 | 56 >>> print stream |
57 <p class="intro">Some text and <a href="http://example.org/">a link</a>.<br/></p> | |
58 | |
59 | |
60 Creating Fragments | |
61 ================== | |
62 | |
63 The ``tag`` object also allows creating “fragments”, which are basically lists | |
64 of nodes (elements or text) that don't have a parent element. This can be useful | |
65 for creating snippets of markup that are attached to a parent element later (for | |
235 | 66 example in a template). Fragments are created by calling the ``tag`` object:: |
226 | 67 |
382
2682dabbcd04
* Added documentation for the various stream event kinds.
cmlenz
parents:
235
diff
changeset
|
68 >>> fragment = tag('Hello, ', tag.em('world'), '!') |
226 | 69 >>> fragment |
70 <Fragment> | |
71 >>> print fragment | |
72 Hello, <em>world</em>! |