annotate doc/xml-templates.txt @ 451:bcc1d6c67a2f

Fix broken link in documentation.
author cmlenz
date Sat, 14 Apr 2007 22:29:09 +0000
parents ff7c72b52fb2
children ca7d707d51b0
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
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 429
diff changeset
21 rendered, and template expressions that are dynamically substituted by
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
22 variable data.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
23
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 429
diff changeset
24 See `Genshi Templating Basics <templates.html>`_ for general information on
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 429
diff changeset
25 embedding Python code in templates.
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 429
diff changeset
26
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
27
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
28 .. contents:: Contents
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
29 :depth: 3
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
30 .. sectnum::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
31
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
32
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
33 .. _`directives`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
34
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
35 -------------------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
36 Template Directives
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
37 -------------------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
38
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
39 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
40 by the namespace ``http://genshi.edgewall.org/``. They can affect how the
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
41 template is rendered in a number of ways: Genshi provides directives for
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
42 conditionals and looping, among others.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
43
394
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
44 To use directives in a template, the namespace must be declared, which is
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
45 usually done on the root element::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
46
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
47 <html xmlns="http://www.w3.org/1999/xhtml"
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
48 xmlns:py="http://genshi.edgewall.org/"
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
49 lang="en">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
50 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
51 </html>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
52
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
53 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
54 namespace for Genshi directives is bound to the prefix “py”.
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
55
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
56 All directives can be applied as attributes, and some can also be used as
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
57 elements. The ``if`` directives for conditionals, for example, can be used in
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
58 both ways::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
59
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
60 <html xmlns="http://www.w3.org/1999/xhtml"
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
61 xmlns:py="http://genshi.edgewall.org/"
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
62 lang="en">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
63 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
64 <div py:if="foo">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
65 <p>Bar</p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
66 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
67 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
68 </html>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
69
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
70 This is basically equivalent to the following::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
71
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
72 <html xmlns="http://www.w3.org/1999/xhtml"
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
73 xmlns:py="http://genshi.edgewall.org/"
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
74 lang="en">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
75 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
76 <py:if test="foo">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
77 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
78 <p>Bar</p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
79 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
80 </py:if>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
81 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
82 </html>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
83
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
84 The rationale behind the second form is that directives do not always map
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
85 naturally to elements in the template. In such cases, the ``py:strip``
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
86 directive can be used to strip off the unwanted element, or the directive can
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
87 simply be used as an element.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
88
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
89
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
90 Conditional Sections
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
91 ====================
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
92
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
93 .. _`py:if`:
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
94
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
95 ``py:if``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
96 ---------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
97
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
98 The element is only rendered if the expression evaluates to a truth value::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
99
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
100 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
101 <b py:if="foo">${bar}</b>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
102 </div>
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
103
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
104 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
105 would produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
106
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
107 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
108 <b>Hello</b>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
109 </div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
110
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
111 This directive can also be used as an element::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
112
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
113 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
114 <py:if test="foo">
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
115 <b>${bar}</b>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
116 </py:if>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
117 </div>
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
118
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
119 .. _`py:choose`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
120 .. _`py:when`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
121 .. _`py:otherwise`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
122
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
123 ``py:choose``
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
124 -------------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
125
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
126 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
127 and ``py:otherwise`` provides advanced conditional processing for rendering one
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
128 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
129 if no ``py:when`` branch matches, the ``py:otherwise`` branch is rendered.
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
130
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
131 If the ``py:choose`` directive is empty the nested ``py:when`` directives will
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
132 be tested for truth::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
133
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
134 <div py:choose="">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
135 <span py:when="0 == 1">0</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
136 <span py:when="1 == 1">1</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
137 <span py:otherwise="">2</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
138 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
139
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
140 This would produce the following output::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
141
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
142 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
143 <span>1</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
144 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
145
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
146 If the ``py:choose`` directive contains an expression the nested ``py:when``
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
147 directives will be tested for equality to the parent ``py:choose`` value::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
148
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
149 <div py:choose="1">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
150 <span py:when="0">0</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
151 <span py:when="1">1</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
152 <span py:otherwise="">2</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
153 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
154
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
155 This would produce the following output::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
156
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
157 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
158 <span>1</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
159 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
160
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
161
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
162 Looping
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
163 =======
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
164
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
165 .. _`py:for`:
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
166
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
167 ``py:for``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
168 ----------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
169
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
170 The element is repeated for every item in an iterable::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
171
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
172 <ul>
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
173 <li py:for="item in items">${item}</li>
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
174 </ul>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
175
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
176 Given ``items=[1, 2, 3]`` in the context data, this would produce::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
177
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
178 <ul>
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
179 <li>1</li><li>2</li><li>3</li>
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
180 </ul>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
181
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
182 This directive can also be used as an element::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
183
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
184 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
185 <py:for each="item in items">
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
186 <li>${item}</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
187 </py:for>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
188 </ul>
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
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
191 Snippet Reuse
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
192 =============
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
193
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
194 .. _`py:def`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
195 .. _`macros`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
196
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
197 ``py:def``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
198 ----------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
199
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
200 The ``py:def`` directive can be used to create macros, i.e. snippets of
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
201 template code that have a name and optionally some parameters, and that can be
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
202 inserted in other places::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
203
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
204 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
205 <p py:def="greeting(name)" class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
206 Hello, ${name}!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
207 </p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
208 ${greeting('world')}
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
209 ${greeting('everyone else')}
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
210 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
211
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
212 The above would be rendered to::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
213
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
214 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
215 <p class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
216 Hello, world!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
217 </p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
218 <p class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
219 Hello, everyone else!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
220 </p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
221 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
222
394
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
223 If a macro doesn't require parameters, it can be defined without the
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
224 parenthesis. For example::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
225
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
226 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
227 <p py:def="greeting" class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
228 Hello, world!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
229 </p>
394
ebc7c1a3bc4d Minor doc fixes.
cmlenz
parents: 354
diff changeset
230 ${greeting()}
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
231 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
232
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
233 The above would be rendered to::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
234
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
235 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
236 <p class="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
237 Hello, world!
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
238 </p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
239 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
240
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
241 This directive can also be used as an element::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
242
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
243 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
244 <py:def function="greeting(name)">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
245 <p class="greeting">Hello, ${name}!</p>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
246 </py:def>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
247 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
248
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
249
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
250 .. _Match Templates:
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
251 .. _`py:match`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
252
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
253 ``py:match``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
254 ------------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
255
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
256 This directive defines a *match template*: given an XPath expression, it
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
257 replaces any element in the template that matches the expression with its own
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
258 content.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
259
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
260 For example, the match template defined in the following template matches any
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
261 element with the tag name “greeting”::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
262
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
263 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
264 <span py:match="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
265 Hello ${select('@name')}
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
266 </span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
267 <greeting name="Dude" />
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
268 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
269
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
270 This would result in the following output::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
271
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
272 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
273 <span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
274 Hello Dude
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
275 </span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
276 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
277
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
278 Inside the body of a ``py:match`` directive, the ``select(path)`` function is
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
279 made available so that parts or all of the original element can be incorporated
451
bcc1d6c67a2f Fix broken link in documentation.
cmlenz
parents: 442
diff changeset
280 in the output of the match template. See `Using XPath`_ for more information
bcc1d6c67a2f Fix broken link in documentation.
cmlenz
parents: 442
diff changeset
281 about this function.
bcc1d6c67a2f Fix broken link in documentation.
cmlenz
parents: 442
diff changeset
282
bcc1d6c67a2f Fix broken link in documentation.
cmlenz
parents: 442
diff changeset
283 .. _`Using XPath`: streams.html#using-xpath
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
284
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
285 This directive can also be used as an element::
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 <py:match path="greeting">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
289 <span>Hello ${select('@name')}</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
290 </py:match>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
291 <greeting name="Dude" />
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
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
295 Variable Binding
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
296 ================
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
297
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
298 .. _`with`:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
299
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
300 ``py:with``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
301 -----------
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
302
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
303 The ``py:with`` directive lets you assign expressions to variables, which can
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
304 be used to make expressions inside the directive less verbose and more
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
305 efficient. For example, if you need use the expression ``author.posts`` more
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
306 than once, and that actually results in a database query, assigning the results
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
307 to a variable using this directive would probably help.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
308
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
309 For example::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
310
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
311 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
312 <span py:with="y=7; z=x+10">$x $y $z</span>
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 Given ``x=42`` in the context data, this would produce::
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 <span>42 7 52</span>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
319 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
320
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
321 This directive can also be used as an element::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
322
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
323 <div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
324 <py:with vars="y=7; z=x+10">$x $y $z</py:with>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
325 </div>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
326
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
327 Note that if a variable of the same name already existed outside of the scope
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
328 of the ``py:with`` directive, it will **not** be overwritten. Instead, it
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
329 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
330 Effectively, this means that variables are immutable in Genshi.
226
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 Structure Manipulation
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
334 ======================
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
335
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
336 .. _`py:attrs`:
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
337
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
338 ``py:attrs``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
339 ------------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
340
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
341 This directive adds, modifies or removes attributes from the element::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
342
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
343 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
344 <li py:attrs="foo">Bar</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
345 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
346
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
347 Given ``foo={'class': 'collapse'}`` in the template context, this would
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
348 produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
349
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
350 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
351 <li class="collapse">Bar</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
352 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
353
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
354 Attributes with the value ``None`` are omitted, so given ``foo={'class': None}``
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
355 in the context for the same template this would produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
356
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
357 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
358 <li>Bar</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
359 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
360
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
361 This directive can only be used as an attribute.
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
362
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
363
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
364 .. _`py:content`:
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
365
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
366 ``py:content``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
367 --------------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
368
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
369 This directive replaces any nested content with the result of evaluating the
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
370 expression::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
371
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
372 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
373 <li py:content="bar">Hello</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
374 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
375
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
376 Given ``bar='Bye'`` in the context data, this would produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
377
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
378 <ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
379 <li>Bye</li>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
380 </ul>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
381
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
382 This directive can only be used as an attribute.
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
383
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
384
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
385 .. _`py:replace`:
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
386
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
387 ``py:replace``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
388 --------------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
389
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
390 This directive replaces the element itself with the result of evaluating the
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
391 expression::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
392
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
393 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
394 <span py:replace="bar">Hello</span>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
395 </div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
396
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
397 Given ``bar='Bye'`` in the context data, this would produce::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
398
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
399 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
400 Bye
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
401 </div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
402
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
403 This directive can only be used as an attribute.
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
404
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
405
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
406 .. _`py:strip`:
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
407
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
408 ``py:strip``
237
ad52b350e132 Flatten outline of XML templating documentation.
cmlenz
parents: 235
diff changeset
409 ------------
235
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
410
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
411 This directive conditionally strips the top-level element from the output. When
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
412 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
413 stripped from the output::
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
414
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
415 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
416 <div py:strip="True"><b>foo</b></div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
417 </div>
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 This would be rendered as::
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 <div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
422 <b>foo</b>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
423 </div>
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
424
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
425 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
426 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
427
b3cabd49de75 Beautified the HTML docs a bit.
cmlenz
parents: 230
diff changeset
428
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
429 .. _order:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
430
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
431 Processing Order
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
432 ================
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
433
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
434 It is possible to attach multiple directives to a single element, although not
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
435 all combinations make sense. When multiple directives are encountered, they are
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
436 processed in the following order:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
437
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
438 #. `py:def`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
439 #. `py:match`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
440 #. `py:when`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
441 #. `py:otherwise`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
442 #. `py:for`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
443 #. `py:if`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
444 #. `py:choose`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
445 #. `py:with`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
446 #. `py:replace`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
447 #. `py:content`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
448 #. `py:attrs`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
449 #. `py:strip`_
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
450
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
451
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
452 .. _includes:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
453
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
454 --------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
455 Includes
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
456 --------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
457
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
458 To reuse common snippets of template code, you can include other files using
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
459 XInclude_.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
460
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
461 .. _xinclude: http://www.w3.org/TR/xinclude/
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
462
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
463 For this, you need to declare the XInclude namespace (commonly bound to the
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
464 prefix “xi”) and use the ``<xi:include>`` element where you want the external
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
465 file to be pulled in::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
466
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
467 <html xmlns="http://www.w3.org/1999/xhtml"
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
468 xmlns:py="http://genshi.edgewall.org/"
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
469 xmlns:xi="http://www.w3.org/2001/XInclude">
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
470 <xi:include href="base.html" />
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
471 ...
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
472 </html>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
473
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
474 Include paths are relative to the filename of the template currently being
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
475 processed. So if the example above was in the file "``myapp/index.html``"
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
476 (relative to the template search path), the XInclude processor would look for
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
477 the included file at "``myapp/base.html``". You can also use Unix-style
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
478 relative paths, for example "``../base.html``" to look in the parent directory.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
479
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
480 Any content included this way is inserted into the generated output instead of
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
481 the ``<xi:include>`` element. The included template sees the same context data.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
482 `Match templates`_ and `macros`_ in the included template are also available to
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
483 the including template after the point it was included.
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
484
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
485 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
486 not what you want, you can specify fallback content that should be used if the
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
487 include fails. For example, to to make the include above fail silently, you'd
273
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
488 write::
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
489
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
490 <xi:include href="base.html"><xi:fallback /></xi:include>
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
491
273
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
492 See the `XInclude specification`_ for more about fallback content. Note though
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
493 that Genshi currently only supports a small subset of XInclude.
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
494
4afa86e2ad83 Small doc fix.
cmlenz
parents: 244
diff changeset
495 .. _`xinclude specification`: http://www.w3.org/TR/xinclude/
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
496
230
24757b771651 Renamed Markup to Genshi in repository.
cmlenz
parents: 226
diff changeset
497 Incudes in Genshi are fully dynamic: Just like normal attributes, the `href`
442
ff7c72b52fb2 Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents: 429
diff changeset
498 attribute accepts expressions, and directives_ can be used on the
226
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
499 ``<xi:include />`` element just as on any other element, meaning you can do
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
500 things like conditional includes::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
501
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
502 <xi:include href="${name}.html" py:if="not in_popup"
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
503 py:for="name in ('foo', 'bar', 'baz')" />
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
504
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
505
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
506 .. _comments:
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
507
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
508 --------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
509 Comments
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
510 --------
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
511
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
512 Normal XML/HTML comment syntax can be used in templates::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
513
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
514 <!-- this is a comment -->
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
515
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
516 However, such comments get passed through the processing pipeline and are by
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
517 default included in the final output. If that's not desired, prefix the comment
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
518 text with an exclamation mark::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
519
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
520 <!-- !this is a comment too, but one that will be stripped from the output -->
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
521
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
522 Note that it does not matter whether there's whitespace before or after the
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
523 exclamation mark, so the above could also be written as follows::
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
524
09f869a98149 Add reStructuredText documentation files.
cmlenz
parents:
diff changeset
525 <!--! this is a comment too, but one that will be stripped from the output -->
Copyright (C) 2012-2017 Edgewall Software