annotate doc/xml-templates.txt @ 283:5cbeeb72c5d4

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