annotate doc/xml-templates.txt @ 404:ec05506d1bda

Fix a couple of typos in the docs. Closes #99.
author cmlenz
date Wed, 21 Feb 2007 10:04:43 +0000
parents ebc7c1a3bc4d
children 6911f3c5a7e8
rev   line source
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
1 .. -*- mode: rst; encoding: utf-8 -*-
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
2
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
3 ============================
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
4 Genshi XML Template Language
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
5 ============================
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
6
241
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
7 Genshi provides a XML-based template language that is heavily inspired by Kid_,
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
8 which in turn was inspired by a number of existing template languages, namely
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
9 XSLT_, TAL_, and PHP_.
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
10
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
11 .. _kid: http://kid-templating.org/
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
12 .. _python: http://www.python.org/
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
13 .. _xslt: http://www.w3.org/TR/xslt
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
14 .. _tal: http://www.zope.org/Wikis/DevSite/Projects/ZPT/TAL
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
15 .. _php: http://www.php.net/
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
16
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
17 This document describes the template language and will be most useful as
241
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
18 reference to those developing Genshi XML templates. Templates are XML files of
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
19 some kind (such as XHTML) that include processing directives_ (elements or
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
20 attributes identified by a separate namespace) that affect how the template is
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
21 rendered, and template expressions_ that are dynamically substituted by
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
22 variable data.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
23
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
24
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
25 .. contents:: Contents
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
26 :depth: 3
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
27 .. sectnum::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
28
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
29 ----------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
30 Python API
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
31 ----------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
32
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
33 The Python code required for templating with Genshi is generally based on the
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
34 following pattern:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
35
241
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
36 * Attain a ``MarkupTemplate`` object from a string or file object containing
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
37 the template XML source. This can either be done directly, or through a
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
38 ``TemplateLoader`` instance.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
39 * Call the ``generate()`` method of the template, passing any data that should
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
40 be made available to the template as keyword arguments.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
41 * Serialize the resulting stream using its ``render()`` method.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
42
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
43 For example::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
44
241
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
45 from genshi.template import MarkupTemplate
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
46
241
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
47 tmpl = MarkupTemplate('<h1>$title</h1>')
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
48 stream = tmpl.generate(title='Hello, world!')
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
49 print stream.render('xhtml')
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
50
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
51 That code would produce the following output::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
52
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
53 <h1>Hello, world!</h1>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
54
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
55 However, if you want includes_ to work, you should attain the template instance
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
56 through a ``TemplateLoader``, and load the template from a file::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
57
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
58 from genshi.template import TemplateLoader
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
59
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
60 loader = TemplateLoader([templates_dir])
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
61 tmpl = loader.load('test.html')
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
62 stream = tmpl.generate(title='Hello, world!')
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
63 print stream.render('xhtml')
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
64
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
65
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
66 .. _`expressions`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
67
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
68 --------------------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
69 Template Expressions
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
70 --------------------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
71
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
72 Python_ expressions can be used in text and attribute values. An expression is
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
73 substituted with the result of its evaluation against the template data.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
74 Expressions need to prefixed with a dollar sign (``$``) and usually enclosed in
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
75 curly braces (``{…}``).
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
76
394
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
77 If the expression starts with a letter and contains only letters, digits, dots,
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
78 and underscores, the curly braces may be omitted. In all other cases, the
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
79 braces are required so that the template processor knows where the expression
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
80 ends::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
81
241
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
82 >>> from genshi.template import MarkupTemplate
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
83 >>> tmpl = MarkupTemplate('<em>${items[0].capitalize()} item</em>')
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
84 >>> print tmpl.generate(items=['first', 'second'])
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
85 <em>First item</em>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
86
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
87 Expressions support the full power of Python. In addition, it is possible to
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
88 access items in a dictionary using “dotted notation” (i.e. as if they were
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
89 attributes), and vice-versa (i.e. access attributes as if they were items in a
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
90 dictionary)::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
91
241
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
92 >>> from genshi.template import MarkupTemplate
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
93 >>> tmpl = MarkupTemplate('<em>${dict.foo}</em>')
bbed6d426678 * Added basic documentation for the text-based template language.
cmlenz
parents: 237
diff changeset
94 >>> print tmpl.generate(dict={'foo': 'bar'})
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
95 <em>bar</em>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
96
244
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
97 Another difference is that you can access variables that are not defined, and
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
98 won't get a ``NameError`` exception::
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
99
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
100 >>> from genshi.template import TextTemplate
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
101 >>> tmpl = TextTemplate('${doh}')
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
102 >>> print tmpl.generate()
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
103 <BLANKLINE>
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
104
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
105 You **will** however get a ``NameError`` if you try to call an undefined
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
106 variable, or do anything else with it, such as accessing its attributes. If you
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
107 need to know whether a variable is defined, you can check its type against the
354
7ae694e7462e Remove unused code from text template plugin.
cmlenz
parents: 273
diff changeset
108 ``Undefined`` class, for example in a `py:if`_ directive::
244
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
109
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
110 >>> from genshi.template import TextTemplate
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
111 >>> tmpl = TextTemplate('${type(doh) is Undefined}')
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
112 >>> print tmpl.generate()
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
113 True
78ae64ef822e Fixes for the text template docs.
cmlenz
parents: 241
diff changeset
114
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
115
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
116 .. _`directives`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
117
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
118 -------------------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
119 Template Directives
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
120 -------------------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
121
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
122 Directives are elements and/or attributes in the template that are identified
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
123 by the namespace ``http://genshi.edgewall.org/``. They can affect how the
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
124 template is rendered in a number of ways: Genshi provides directives for
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
125 conditionals and looping, among others.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
126
394
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
127 To use directives in a template, the namespace must be declared, which is
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
128 usually done on the root element::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
129
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
130 <html xmlns="http://www.w3.org/1999/xhtml"
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
131 xmlns:py="http://genshi.edgewall.org/"
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
132 lang="en">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
133 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
134 </html>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
135
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
136 In this example, the default namespace is set to the XHTML namespace, and the
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
137 namespace for Genshi directives is bound to the prefix “py”.
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
138
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
139 All directives can be applied as attributes, and some can also be used as
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
140 elements. The ``if`` directives for conditionals, for example, can be used in
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
141 both ways::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
142
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
143 <html xmlns="http://www.w3.org/1999/xhtml"
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
144 xmlns:py="http://genshi.edgewall.org/"
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
145 lang="en">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
146 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
147 <div py:if="foo">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
148 <p>Bar</p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
149 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
150 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
151 </html>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
152
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
153 This is basically equivalent to the following::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
154
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
155 <html xmlns="http://www.w3.org/1999/xhtml"
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
156 xmlns:py="http://genshi.edgewall.org/"
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
157 lang="en">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
158 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
159 <py:if test="foo">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
160 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
161 <p>Bar</p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
162 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
163 </py:if>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
164 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
165 </html>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
166
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
167 The rationale behind the second form is that directives do not always map
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
168 naturally to elements in the template. In such cases, the ``py:strip``
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
169 directive can be used to strip off the unwanted element, or the directive can
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
170 simply be used as an element.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
171
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
172
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
173 Conditional Sections
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
174 ====================
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
175
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
176 .. _`py:if`:
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
177
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
178 ``py:if``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
179 ---------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
180
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
181 The element is only rendered if the expression evaluates to a truth value::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
182
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
183 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
184 <b py:if="foo">${bar}</b>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
185 </div>
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
186
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
187 Given the data ``foo=True`` and ``bar='Hello'`` in the template context, this
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
188 would produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
189
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
190 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
191 <b>Hello</b>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
192 </div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
193
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
194 This directive can also be used as an element::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
195
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
196 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
197 <py:if test="foo">
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
198 <b>${bar}</b>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
199 </py:if>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
200 </div>
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
201
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
202 .. _`py:choose`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
203 .. _`py:when`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
204 .. _`py:otherwise`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
205
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
206 ``py:choose``
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
207 -------------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
208
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
209 The ``py:choose`` directive, in combination with the directives ``py:when``
404
ec05506d1bda Fix a couple of typos in the docs. Closes #99.
cmlenz
parents: 394
diff changeset
210 and ``py:otherwise`` provides advanced conditional processing for rendering one
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
211 of several alternatives. The first matching ``py:when`` branch is rendered, or,
404
ec05506d1bda Fix a couple of typos in the docs. Closes #99.
cmlenz
parents: 394
diff changeset
212 if no ``py:when`` branch matches, the ``py:otherwise`` branch is rendered.
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
213
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
214 If the ``py:choose`` directive is empty the nested ``py:when`` directives will
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
215 be tested for truth::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
216
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
217 <div py:choose="">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
218 <span py:when="0 == 1">0</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
219 <span py:when="1 == 1">1</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
220 <span py:otherwise="">2</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
221 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
222
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
223 This would produce the following output::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
224
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
225 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
226 <span>1</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
227 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
228
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
229 If the ``py:choose`` directive contains an expression the nested ``py:when``
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
230 directives will be tested for equality to the parent ``py:choose`` value::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
231
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
232 <div py:choose="1">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
233 <span py:when="0">0</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
234 <span py:when="1">1</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
235 <span py:otherwise="">2</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
236 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
237
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
238 This would produce the following output::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
239
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
240 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
241 <span>1</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
242 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
243
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
244
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
245 Looping
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
246 =======
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
247
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
248 .. _`py:for`:
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
249
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
250 ``py:for``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
251 ----------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
252
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
253 The element is repeated for every item in an iterable::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
254
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
255 <ul>
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
256 <li py:for="item in items">${item}</li>
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
257 </ul>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
258
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
259 Given ``items=[1, 2, 3]`` in the context data, this would produce::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
260
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
261 <ul>
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
262 <li>1</li><li>2</li><li>3</li>
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
263 </ul>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
264
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
265 This directive can also be used as an element::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
266
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
267 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
268 <py:for each="item in items">
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
269 <li>${item}</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
270 </py:for>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
271 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
272
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
273
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
274 Snippet Reuse
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
275 =============
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
276
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
277 .. _`py:def`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
278 .. _`macros`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
279
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
280 ``py:def``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
281 ----------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
282
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
283 The ``py:def`` directive can be used to create macros, i.e. snippets of
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
284 template code that have a name and optionally some parameters, and that can be
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
285 inserted in other places::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
286
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
287 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
288 <p py:def="greeting(name)" class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
289 Hello, ${name}!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
290 </p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
291 ${greeting('world')}
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
292 ${greeting('everyone else')}
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
293 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
294
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
295 The above would be rendered to::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
296
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
297 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
298 <p class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
299 Hello, world!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
300 </p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
301 <p class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
302 Hello, everyone else!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
303 </p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
304 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
305
394
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
306 If a macro doesn't require parameters, it can be defined without the
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
307 parenthesis. For example::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
308
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
309 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
310 <p py:def="greeting" class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
311 Hello, world!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
312 </p>
394
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
313 ${greeting()}
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
314 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
315
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
316 The above would be rendered to::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
317
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
318 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
319 <p class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
320 Hello, world!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
321 </p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
322 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
323
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
324 This directive can also be used as an element::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
325
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
326 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
327 <py:def function="greeting(name)">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
328 <p class="greeting">Hello, ${name}!</p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
329 </py:def>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
330 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
331
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
332
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
333 .. _Match Templates:
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
334 .. _`py:match`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
335
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
336 ``py:match``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
337 ------------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
338
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
339 This directive defines a *match template*: given an XPath expression, it
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
340 replaces any element in the template that matches the expression with its own
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
341 content.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
342
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
343 For example, the match template defined in the following template matches any
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
344 element with the tag name “greeting”::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
345
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
346 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
347 <span py:match="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
348 Hello ${select('@name')}
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
349 </span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
350 <greeting name="Dude" />
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
351 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
352
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
353 This would result in the following output::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
354
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
355 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
356 <span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
357 Hello Dude
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
358 </span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
359 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
360
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
361 Inside the body of a ``py:match`` directive, the ``select(path)`` function is
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
362 made available so that parts or all of the original element can be incorporated
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
363 in the output of the match template. See [wiki:GenshiStream#UsingXPath] for
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
364 more information about this function.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
365
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
366 This directive can also be used as an element::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
367
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
368 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
369 <py:match path="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
370 <span>Hello ${select('@name')}</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
371 </py:match>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
372 <greeting name="Dude" />
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
373 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
374
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
375
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
376 Variable Binding
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
377 ================
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
378
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
379 .. _`with`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
380
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
381 ``py:with``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
382 -----------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
383
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
384 The ``py:with`` directive lets you assign expressions to variables, which can
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
385 be used to make expressions inside the directive less verbose and more
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
386 efficient. For example, if you need use the expression ``author.posts`` more
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
387 than once, and that actually results in a database query, assigning the results
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
388 to a variable using this directive would probably help.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
389
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
390 For example::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
391
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
392 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
393 <span py:with="y=7; z=x+10">$x $y $z</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
394 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
395
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
396 Given ``x=42`` in the context data, this would produce::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
397
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
398 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
399 <span>42 7 52</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
400 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
401
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
402 This directive can also be used as an element::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
403
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
404 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
405 <py:with vars="y=7; z=x+10">$x $y $z</py:with>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
406 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
407
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
408 Note that if a variable of the same name already existed outside of the scope
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
409 of the ``py:with`` directive, it will **not** be overwritten. Instead, it
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
410 will have the same value it had prior to the ``py:with`` assignment.
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
411 Effectively, this means that variables are immutable in Genshi.
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
412
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
413
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
414 Structure Manipulation
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
415 ======================
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
416
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
417 .. _`py:attrs`:
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
418
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
419 ``py:attrs``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
420 ------------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
421
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
422 This directive adds, modifies or removes attributes from the element::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
423
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
424 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
425 <li py:attrs="foo">Bar</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
426 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
427
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
428 Given ``foo={'class': 'collapse'}`` in the template context, this would
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
429 produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
430
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
431 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
432 <li class="collapse">Bar</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
433 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
434
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
435 Attributes with the value ``None`` are omitted, so given ``foo={'class': None}``
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
436 in the context for the same template this would produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
437
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
438 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
439 <li>Bar</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
440 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
441
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
442 This directive can only be used as an attribute.
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
443
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
444
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
445 .. _`py:content`:
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
446
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
447 ``py:content``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
448 --------------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
449
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
450 This directive replaces any nested content with the result of evaluating the
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
451 expression::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
452
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
453 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
454 <li py:content="bar">Hello</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
455 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
456
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
457 Given ``bar='Bye'`` in the context data, this would produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
458
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
459 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
460 <li>Bye</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
461 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
462
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
463 This directive can only be used as an attribute.
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
464
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
465
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
466 .. _`py:replace`:
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
467
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
468 ``py:replace``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
469 --------------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
470
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
471 This directive replaces the element itself with the result of evaluating the
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
472 expression::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
473
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
474 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
475 <span py:replace="bar">Hello</span>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
476 </div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
477
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
478 Given ``bar='Bye'`` in the context data, this would produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
479
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
480 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
481 Bye
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
482 </div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
483
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
484 This directive can only be used as an attribute.
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
485
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
486
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
487 .. _`py:strip`:
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
488
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
489 ``py:strip``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
490 ------------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
491
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
492 This directive conditionally strips the top-level element from the output. When
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
493 the value of the ``py:strip`` attribute evaluates to ``True``, the element is
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
494 stripped from the output::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
495
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
496 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
497 <div py:strip="True"><b>foo</b></div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
498 </div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
499
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
500 This would be rendered as::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
501
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
502 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
503 <b>foo</b>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
504 </div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
505
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
506 As a shorthand, if the value of the ``py:strip`` attribute is empty, that has
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
507 the same effect as using a truth value (i.e. the element is stripped).
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
508
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
509
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
510 .. _order:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
511
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
512 Processing Order
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
513 ================
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
514
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
515 It is possible to attach multiple directives to a single element, although not
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
516 all combinations make sense. When multiple directives are encountered, they are
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
517 processed in the following order:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
518
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
519 #. `py:def`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
520 #. `py:match`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
521 #. `py:when`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
522 #. `py:otherwise`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
523 #. `py:for`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
524 #. `py:if`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
525 #. `py:choose`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
526 #. `py:with`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
527 #. `py:replace`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
528 #. `py:content`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
529 #. `py:attrs`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
530 #. `py:strip`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
531
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
532
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
533 .. _includes:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
534
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
535 --------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
536 Includes
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
537 --------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
538
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
539 To reuse common snippets of template code, you can include other files using
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
540 XInclude_.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
541
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
542 .. _xinclude: http://www.w3.org/TR/xinclude/
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
543
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
544 For this, you need to declare the XInclude namespace (commonly bound to the
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
545 prefix “xi”) and use the ``<xi:include>`` element where you want the external
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
546 file to be pulled in::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
547
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
548 <html xmlns="http://www.w3.org/1999/xhtml"
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
549 xmlns:py="http://genshi.edgewall.org/"
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
550 xmlns:xi="http://www.w3.org/2001/XInclude">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
551 <xi:include href="base.html" />
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
552 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
553 </html>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
554
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
555 Include paths are relative to the filename of the template currently being
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
556 processed. So if the example above was in the file "``myapp/index.html``"
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
557 (relative to the template search path), the XInclude processor would look for
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
558 the included file at "``myapp/base.html``". You can also use Unix-style
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
559 relative paths, for example "``../base.html``" to look in the parent directory.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
560
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
561 Any content included this way is inserted into the generated output instead of
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
562 the ``<xi:include>`` element. The included template sees the same context data.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
563 `Match templates`_ and `macros`_ in the included template are also available to
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
564 the including template after the point it was included.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
565
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
566 By default, an error will be raised if an included file is not found. If that's
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
567 not what you want, you can specify fallback content that should be used if the
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
568 include fails. For example, to to make the include above fail silently, you'd
273
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
569 write::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
570
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
571 <xi:include href="base.html"><xi:fallback /></xi:include>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
572
273
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
573 See the `XInclude specification`_ for more about fallback content. Note though
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
574 that Genshi currently only supports a small subset of XInclude.
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
575
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
576 .. _`xinclude specification`: http://www.w3.org/TR/xinclude/
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
577
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
578 Incudes in Genshi are fully dynamic: Just like normal attributes, the `href`
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
579 attribute accepts expressions_, and directives_ can be used on the
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
580 ``<xi:include />`` element just as on any other element, meaning you can do
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
581 things like conditional includes::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
582
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
583 <xi:include href="${name}.html" py:if="not in_popup"
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
584 py:for="name in ('foo', 'bar', 'baz')" />
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
585
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
586
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
587 .. _comments:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
588
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
589 --------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
590 Comments
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
591 --------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
592
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
593 Normal XML/HTML comment syntax can be used in templates::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
594
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
595 <!-- this is a comment -->
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
596
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
597 However, such comments get passed through the processing pipeline and are by
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
598 default included in the final output. If that's not desired, prefix the comment
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
599 text with an exclamation mark::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
600
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
601 <!-- !this is a comment too, but one that will be stripped from the output -->
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
602
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
603 Note that it does not matter whether there's whitespace before or after the
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
604 exclamation mark, so the above could also be written as follows::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
605
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
606 <!--! this is a comment too, but one that will be stripped from the output -->
Copyright (C) 2012-2017 Edgewall Software