Mercurial > genshi > genshi-test
annotate doc/xml-templates.txt @ 725:c2af5d7d2161 experimental-optimizer
Note that `py:choose` and friends may be used as elements.
author | athomas |
---|---|
date | Sun, 04 May 2008 02:45:59 +0000 |
parents | 69b3b6ca968b |
children |
rev | line source |
---|---|
226 | 1 .. -*- mode: rst; encoding: utf-8 -*- |
2 | |
3 ============================ | |
230 | 4 Genshi XML Template Language |
226 | 5 ============================ |
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 | 10 |
11 .. _kid: http://kid-templating.org/ | |
12 .. _python: http://www.python.org/ | |
13 .. _xslt: http://www.w3.org/TR/xslt | |
14 .. _tal: http://www.zope.org/Wikis/DevSite/Projects/ZPT/TAL | |
15 .. _php: http://www.php.net/ | |
16 | |
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 | 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 | 22 variable data. |
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 | 27 |
28 .. contents:: Contents | |
29 :depth: 3 | |
30 .. sectnum:: | |
31 | |
32 | |
33 .. _`directives`: | |
34 | |
35 ------------------- | |
36 Template Directives | |
37 ------------------- | |
38 | |
39 Directives are elements and/or attributes in the template that are identified | |
230 | 40 by the namespace ``http://genshi.edgewall.org/``. They can affect how the |
41 template is rendered in a number of ways: Genshi provides directives for | |
226 | 42 conditionals and looping, among others. |
43 | |
394 | 44 To use directives in a template, the namespace must be declared, which is |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
45 usually done on the root element: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
46 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
47 .. code-block:: genshi |
226 | 48 |
49 <html xmlns="http://www.w3.org/1999/xhtml" | |
230 | 50 xmlns:py="http://genshi.edgewall.org/" |
226 | 51 lang="en"> |
52 ... | |
53 </html> | |
54 | |
55 In this example, the default namespace is set to the XHTML namespace, and the | |
230 | 56 namespace for Genshi directives is bound to the prefix “py”. |
226 | 57 |
58 All directives can be applied as attributes, and some can also be used as | |
59 elements. The ``if`` directives for conditionals, for example, can be used in | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
60 both ways: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
61 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
62 .. code-block:: genshi |
226 | 63 |
64 <html xmlns="http://www.w3.org/1999/xhtml" | |
230 | 65 xmlns:py="http://genshi.edgewall.org/" |
226 | 66 lang="en"> |
67 ... | |
68 <div py:if="foo"> | |
69 <p>Bar</p> | |
70 </div> | |
71 ... | |
72 </html> | |
73 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
74 This is basically equivalent to the following: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
75 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
76 .. code-block:: genshi |
226 | 77 |
78 <html xmlns="http://www.w3.org/1999/xhtml" | |
230 | 79 xmlns:py="http://genshi.edgewall.org/" |
226 | 80 lang="en"> |
81 ... | |
82 <py:if test="foo"> | |
83 <div> | |
84 <p>Bar</p> | |
85 </div> | |
86 </py:if> | |
87 ... | |
88 </html> | |
89 | |
90 The rationale behind the second form is that directives do not always map | |
91 naturally to elements in the template. In such cases, the ``py:strip`` | |
92 directive can be used to strip off the unwanted element, or the directive can | |
93 simply be used as an element. | |
94 | |
95 | |
237 | 96 Conditional Sections |
226 | 97 ==================== |
98 | |
235 | 99 .. _`py:if`: |
226 | 100 |
235 | 101 ``py:if`` |
237 | 102 --------- |
226 | 103 |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
104 The element is only rendered if the expression evaluates to a truth value: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
105 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
106 .. code-block:: genshi |
226 | 107 |
235 | 108 <div> |
109 <b py:if="foo">${bar}</b> | |
110 </div> | |
226 | 111 |
235 | 112 Given the data ``foo=True`` and ``bar='Hello'`` in the template context, this |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
113 would produce: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
114 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
115 .. code-block:: xml |
235 | 116 |
117 <div> | |
118 <b>Hello</b> | |
119 </div> | |
120 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
121 This directive can also be used as an element: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
122 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
123 .. code-block:: genshi |
235 | 124 |
125 <div> | |
126 <py:if test="foo"> | |
127 <b>${bar}</b> | |
128 </py:if> | |
129 </div> | |
226 | 130 |
131 .. _`py:choose`: | |
132 .. _`py:when`: | |
133 .. _`py:otherwise`: | |
134 | |
237 | 135 ``py:choose`` |
136 ------------- | |
226 | 137 |
237 | 138 The ``py:choose`` directive, in combination with the directives ``py:when`` |
404 | 139 and ``py:otherwise`` provides advanced conditional processing for rendering one |
226 | 140 of several alternatives. The first matching ``py:when`` branch is rendered, or, |
404 | 141 if no ``py:when`` branch matches, the ``py:otherwise`` branch is rendered. |
226 | 142 |
143 If the ``py:choose`` directive is empty the nested ``py:when`` directives will | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
144 be tested for truth: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
145 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
146 .. code-block:: genshi |
226 | 147 |
148 <div py:choose=""> | |
149 <span py:when="0 == 1">0</span> | |
150 <span py:when="1 == 1">1</span> | |
151 <span py:otherwise="">2</span> | |
152 </div> | |
153 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
154 This would produce the following output: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
155 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
156 .. code-block:: xml |
226 | 157 |
158 <div> | |
159 <span>1</span> | |
160 </div> | |
161 | |
162 If the ``py:choose`` directive contains an expression the nested ``py:when`` | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
163 directives will be tested for equality to the parent ``py:choose`` value: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
164 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
165 .. code-block:: genshi |
226 | 166 |
167 <div py:choose="1"> | |
168 <span py:when="0">0</span> | |
169 <span py:when="1">1</span> | |
170 <span py:otherwise="">2</span> | |
171 </div> | |
172 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
173 This would produce the following output: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
174 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
175 .. code-block:: xml |
226 | 176 |
177 <div> | |
178 <span>1</span> | |
179 </div> | |
180 | |
725
c2af5d7d2161
Note that `py:choose` and friends may be used as elements.
athomas
parents:
657
diff
changeset
|
181 These directives can also be used as elements: |
c2af5d7d2161
Note that `py:choose` and friends may be used as elements.
athomas
parents:
657
diff
changeset
|
182 |
c2af5d7d2161
Note that `py:choose` and friends may be used as elements.
athomas
parents:
657
diff
changeset
|
183 .. code-block:: genshi |
c2af5d7d2161
Note that `py:choose` and friends may be used as elements.
athomas
parents:
657
diff
changeset
|
184 |
c2af5d7d2161
Note that `py:choose` and friends may be used as elements.
athomas
parents:
657
diff
changeset
|
185 <py:choose test="1"> |
c2af5d7d2161
Note that `py:choose` and friends may be used as elements.
athomas
parents:
657
diff
changeset
|
186 <py:when test="0">0</py:when> |
c2af5d7d2161
Note that `py:choose` and friends may be used as elements.
athomas
parents:
657
diff
changeset
|
187 <py:when test="1">1</py:when> |
c2af5d7d2161
Note that `py:choose` and friends may be used as elements.
athomas
parents:
657
diff
changeset
|
188 <py:otherwise>2</py:otherwise> |
c2af5d7d2161
Note that `py:choose` and friends may be used as elements.
athomas
parents:
657
diff
changeset
|
189 </py:choose> |
226 | 190 |
235 | 191 Looping |
237 | 192 ======= |
226 | 193 |
235 | 194 .. _`py:for`: |
226 | 195 |
235 | 196 ``py:for`` |
237 | 197 ---------- |
235 | 198 |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
199 The element is repeated for every item in an iterable: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
200 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
201 .. code-block:: genshi |
226 | 202 |
203 <ul> | |
235 | 204 <li py:for="item in items">${item}</li> |
226 | 205 </ul> |
206 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
207 Given ``items=[1, 2, 3]`` in the context data, this would produce: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
208 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
209 .. code-block:: xml |
226 | 210 |
211 <ul> | |
235 | 212 <li>1</li><li>2</li><li>3</li> |
226 | 213 </ul> |
214 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
215 This directive can also be used as an element: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
216 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
217 .. code-block:: genshi |
226 | 218 |
235 | 219 <ul> |
220 <py:for each="item in items"> | |
221 <li>${item}</li> | |
222 </py:for> | |
223 </ul> | |
224 | |
225 | |
226 Snippet Reuse | |
237 | 227 ============= |
226 | 228 |
229 .. _`py:def`: | |
230 .. _`macros`: | |
231 | |
232 ``py:def`` | |
237 | 233 ---------- |
226 | 234 |
235 The ``py:def`` directive can be used to create macros, i.e. snippets of | |
236 template code that have a name and optionally some parameters, and that can be | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
237 inserted in other places: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
238 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
239 .. code-block:: genshi |
226 | 240 |
241 <div> | |
242 <p py:def="greeting(name)" class="greeting"> | |
243 Hello, ${name}! | |
244 </p> | |
245 ${greeting('world')} | |
246 ${greeting('everyone else')} | |
247 </div> | |
248 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
249 The above would be rendered to: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
250 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
251 .. code-block:: xml |
226 | 252 |
253 <div> | |
254 <p class="greeting"> | |
255 Hello, world! | |
256 </p> | |
257 <p class="greeting"> | |
258 Hello, everyone else! | |
259 </p> | |
260 </div> | |
261 | |
394 | 262 If a macro doesn't require parameters, it can be defined without the |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
263 parenthesis. For example: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
264 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
265 .. code-block:: genshi |
226 | 266 |
267 <div> | |
268 <p py:def="greeting" class="greeting"> | |
269 Hello, world! | |
270 </p> | |
394 | 271 ${greeting()} |
226 | 272 </div> |
273 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
274 The above would be rendered to: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
275 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
276 .. code-block:: xml |
226 | 277 |
278 <div> | |
279 <p class="greeting"> | |
280 Hello, world! | |
281 </p> | |
282 </div> | |
283 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
284 This directive can also be used as an element: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
285 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
286 .. code-block:: genshi |
226 | 287 |
288 <div> | |
289 <py:def function="greeting(name)"> | |
290 <p class="greeting">Hello, ${name}!</p> | |
291 </py:def> | |
292 </div> | |
293 | |
294 | |
235 | 295 .. _Match Templates: |
226 | 296 .. _`py:match`: |
297 | |
298 ``py:match`` | |
237 | 299 ------------ |
226 | 300 |
301 This directive defines a *match template*: given an XPath expression, it | |
302 replaces any element in the template that matches the expression with its own | |
303 content. | |
304 | |
305 For example, the match template defined in the following template matches any | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
306 element with the tag name “greeting”: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
307 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
308 .. code-block:: genshi |
226 | 309 |
310 <div> | |
311 <span py:match="greeting"> | |
312 Hello ${select('@name')} | |
313 </span> | |
314 <greeting name="Dude" /> | |
315 </div> | |
316 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
317 This would result in the following output: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
318 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
319 .. code-block:: xml |
226 | 320 |
321 <div> | |
322 <span> | |
323 Hello Dude | |
324 </span> | |
325 </div> | |
326 | |
327 Inside the body of a ``py:match`` directive, the ``select(path)`` function is | |
328 made available so that parts or all of the original element can be incorporated | |
451 | 329 in the output of the match template. See `Using XPath`_ for more information |
330 about this function. | |
331 | |
332 .. _`Using XPath`: streams.html#using-xpath | |
226 | 333 |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
334 This directive can also be used as an element: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
335 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
336 .. code-block:: genshi |
226 | 337 |
338 <div> | |
339 <py:match path="greeting"> | |
340 <span>Hello ${select('@name')}</span> | |
341 </py:match> | |
342 <greeting name="Dude" /> | |
343 </div> | |
344 | |
602 | 345 When used this way, the ``py:match`` directive can also be annotated with a |
346 couple of optimization hints. For example, the following informs the matching | |
347 engine that the match should only be applied once: | |
348 | |
349 .. code-block:: genshi | |
350 | |
351 <py:match path="body" once="true"> | |
352 <body py:attrs="select('@*')"> | |
353 <div id="header">...</div> | |
354 ${select("*|text()")} | |
355 <div id="footer">...</div> | |
356 </body> | |
357 </py:match> | |
358 | |
359 The following optimization hints are recognized: | |
360 | |
361 +---------------+-----------+-----------------------------------------------+ | |
362 | Attribute | Default | Description | | |
363 +===============+===========+===============================================+ | |
364 | ``once`` | ``false`` | Whether the engine should stop looking for | | |
365 | | | more matching elements after the first match. | | |
366 | | | Use this on match templates that match | | |
367 | | | elements that can only occur once in the | | |
368 | | | stream, such as the ``<head>`` or ``<body>`` | | |
369 | | | elements in an HTML template, or elements | | |
370 | | | with a specific ID. | | |
371 +---------------+-----------+-----------------------------------------------+ | |
372 | ``recursive`` | ``true`` | Whether the match template should be applied | | |
373 | | | to its own output. Note that ``once`` implies | | |
374 | | | non-recursive behavior, so this attribute | | |
375 | | | only needs to be set for match templates that | | |
376 | | | don't also have ``once`` set. | | |
377 +---------------+-----------+-----------------------------------------------+ | |
378 | |
379 .. note:: The ``py:match`` optimization hints were added in the 0.5 release. In | |
380 earlier versions, the attributes have no effect. | |
381 | |
226 | 382 |
235 | 383 Variable Binding |
237 | 384 ================ |
226 | 385 |
386 .. _`with`: | |
387 | |
388 ``py:with`` | |
237 | 389 ----------- |
226 | 390 |
391 The ``py:with`` directive lets you assign expressions to variables, which can | |
392 be used to make expressions inside the directive less verbose and more | |
393 efficient. For example, if you need use the expression ``author.posts`` more | |
394 than once, and that actually results in a database query, assigning the results | |
395 to a variable using this directive would probably help. | |
396 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
397 For example: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
398 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
399 .. code-block:: genshi |
226 | 400 |
401 <div> | |
402 <span py:with="y=7; z=x+10">$x $y $z</span> | |
403 </div> | |
404 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
405 Given ``x=42`` in the context data, this would produce: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
406 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
407 .. code-block:: xml |
226 | 408 |
409 <div> | |
410 <span>42 7 52</span> | |
411 </div> | |
412 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
413 This directive can also be used as an element: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
414 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
415 .. code-block:: genshi |
226 | 416 |
417 <div> | |
418 <py:with vars="y=7; z=x+10">$x $y $z</py:with> | |
419 </div> | |
420 | |
421 Note that if a variable of the same name already existed outside of the scope | |
422 of the ``py:with`` directive, it will **not** be overwritten. Instead, it | |
423 will have the same value it had prior to the ``py:with`` assignment. | |
230 | 424 Effectively, this means that variables are immutable in Genshi. |
226 | 425 |
426 | |
235 | 427 Structure Manipulation |
237 | 428 ====================== |
235 | 429 |
430 .. _`py:attrs`: | |
431 | |
432 ``py:attrs`` | |
237 | 433 ------------ |
235 | 434 |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
435 This directive adds, modifies or removes attributes from the element: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
436 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
437 .. code-block:: genshi |
235 | 438 |
439 <ul> | |
440 <li py:attrs="foo">Bar</li> | |
441 </ul> | |
442 | |
443 Given ``foo={'class': 'collapse'}`` in the template context, this would | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
444 produce: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
445 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
446 .. code-block:: xml |
235 | 447 |
448 <ul> | |
449 <li class="collapse">Bar</li> | |
450 </ul> | |
451 | |
452 Attributes with the value ``None`` are omitted, so given ``foo={'class': None}`` | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
453 in the context for the same template this would produce: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
454 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
455 .. code-block:: xml |
235 | 456 |
457 <ul> | |
458 <li>Bar</li> | |
459 </ul> | |
460 | |
461 This directive can only be used as an attribute. | |
462 | |
463 | |
464 .. _`py:content`: | |
465 | |
466 ``py:content`` | |
237 | 467 -------------- |
235 | 468 |
469 This directive replaces any nested content with the result of evaluating the | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
470 expression: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
471 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
472 .. code-block:: genshi |
235 | 473 |
474 <ul> | |
475 <li py:content="bar">Hello</li> | |
476 </ul> | |
477 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
478 Given ``bar='Bye'`` in the context data, this would produce: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
479 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
480 .. code-block:: xml |
235 | 481 |
482 <ul> | |
483 <li>Bye</li> | |
484 </ul> | |
485 | |
486 This directive can only be used as an attribute. | |
487 | |
488 | |
489 .. _`py:replace`: | |
490 | |
491 ``py:replace`` | |
237 | 492 -------------- |
235 | 493 |
494 This directive replaces the element itself with the result of evaluating the | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
495 expression: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
496 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
497 .. code-block:: genshi |
235 | 498 |
499 <div> | |
500 <span py:replace="bar">Hello</span> | |
501 </div> | |
502 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
503 Given ``bar='Bye'`` in the context data, this would produce: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
504 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
505 .. code-block:: xml |
235 | 506 |
507 <div> | |
508 Bye | |
509 </div> | |
510 | |
657 | 511 This directive can also be used as an element (since version 0.5): |
512 | |
513 .. code-block:: genshi | |
514 | |
515 <div> | |
516 <py:replace value="title">Placeholder</py:replace> | |
517 </div> | |
518 | |
235 | 519 |
520 | |
521 .. _`py:strip`: | |
522 | |
523 ``py:strip`` | |
237 | 524 ------------ |
235 | 525 |
526 This directive conditionally strips the top-level element from the output. When | |
527 the value of the ``py:strip`` attribute evaluates to ``True``, the element is | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
528 stripped from the output: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
529 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
530 .. code-block:: genshi |
235 | 531 |
532 <div> | |
533 <div py:strip="True"><b>foo</b></div> | |
534 </div> | |
535 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
536 This would be rendered as: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
537 |
612
09de73cae3d5
Using `html` code-blocks for examples isn't so nice when viewing the docs over Trac, so change them to `xml`.
cmlenz
parents:
610
diff
changeset
|
538 .. code-block:: xml |
235 | 539 |
540 <div> | |
541 <b>foo</b> | |
542 </div> | |
543 | |
544 As a shorthand, if the value of the ``py:strip`` attribute is empty, that has | |
545 the same effect as using a truth value (i.e. the element is stripped). | |
546 | |
547 | |
226 | 548 .. _order: |
549 | |
550 Processing Order | |
551 ================ | |
552 | |
553 It is possible to attach multiple directives to a single element, although not | |
554 all combinations make sense. When multiple directives are encountered, they are | |
555 processed in the following order: | |
556 | |
557 #. `py:def`_ | |
558 #. `py:match`_ | |
559 #. `py:when`_ | |
560 #. `py:otherwise`_ | |
561 #. `py:for`_ | |
562 #. `py:if`_ | |
563 #. `py:choose`_ | |
564 #. `py:with`_ | |
565 #. `py:replace`_ | |
566 #. `py:content`_ | |
567 #. `py:attrs`_ | |
568 #. `py:strip`_ | |
569 | |
570 | |
571 .. _includes: | |
572 | |
573 -------- | |
574 Includes | |
575 -------- | |
576 | |
577 To reuse common snippets of template code, you can include other files using | |
578 XInclude_. | |
579 | |
580 .. _xinclude: http://www.w3.org/TR/xinclude/ | |
581 | |
582 For this, you need to declare the XInclude namespace (commonly bound to the | |
583 prefix “xi”) and use the ``<xi:include>`` element where you want the external | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
584 file to be pulled in: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
585 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
586 .. code-block:: genshi |
226 | 587 |
588 <html xmlns="http://www.w3.org/1999/xhtml" | |
230 | 589 xmlns:py="http://genshi.edgewall.org/" |
226 | 590 xmlns:xi="http://www.w3.org/2001/XInclude"> |
591 <xi:include href="base.html" /> | |
592 ... | |
593 </html> | |
594 | |
595 Include paths are relative to the filename of the template currently being | |
596 processed. So if the example above was in the file "``myapp/index.html``" | |
597 (relative to the template search path), the XInclude processor would look for | |
598 the included file at "``myapp/base.html``". You can also use Unix-style | |
599 relative paths, for example "``../base.html``" to look in the parent directory. | |
600 | |
601 Any content included this way is inserted into the generated output instead of | |
602 the ``<xi:include>`` element. The included template sees the same context data. | |
603 `Match templates`_ and `macros`_ in the included template are also available to | |
604 the including template after the point it was included. | |
605 | |
606 By default, an error will be raised if an included file is not found. If that's | |
607 not what you want, you can specify fallback content that should be used if the | |
608 include fails. For example, to to make the include above fail silently, you'd | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
609 write: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
610 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
611 .. code-block:: genshi |
226 | 612 |
613 <xi:include href="base.html"><xi:fallback /></xi:include> | |
614 | |
273 | 615 See the `XInclude specification`_ for more about fallback content. Note though |
616 that Genshi currently only supports a small subset of XInclude. | |
617 | |
618 .. _`xinclude specification`: http://www.w3.org/TR/xinclude/ | |
226 | 619 |
610
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
620 |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
621 Dynamic Includes |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
622 ================ |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
623 |
230 | 624 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
|
625 attribute accepts expressions, and directives_ can be used on the |
226 | 626 ``<xi:include />`` element just as on any other element, meaning you can do |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
627 things like conditional includes: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
628 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
629 .. code-block:: genshi |
226 | 630 |
631 <xi:include href="${name}.html" py:if="not in_popup" | |
632 py:for="name in ('foo', 'bar', 'baz')" /> | |
633 | |
634 | |
610
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
635 Including Text Templates |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
636 ======================== |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
637 |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
638 The ``parse`` attribute of the ``<xi:include>`` element can be used to specify |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
639 whether the included template is an XML template or a text template (using the |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
640 new syntax added in Genshi 0.5): |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
641 |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
642 .. code-block:: genshi |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
643 |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
644 <xi:include href="myscript.js" parse="text" /> |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
645 |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
646 This example would load the ``myscript.js`` file as a ``NewTextTemplate``. See |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
647 `text templates`_ for details on the syntax of text templates. |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
648 |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
649 .. _`text templates`: text-templates.html |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
650 |
6a37018199fd
* XInclude elements in markup templates now support the `parse` attribute; when set to "xml" (the default), the include is processed as before, but when set to "text", the included template is parsed as a text template using the new syntax (ticket #101).
cmlenz
parents:
602
diff
changeset
|
651 |
226 | 652 .. _comments: |
653 | |
654 -------- | |
655 Comments | |
656 -------- | |
657 | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
658 Normal XML/HTML comment syntax can be used in templates: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
659 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
660 .. code-block:: genshi |
226 | 661 |
662 <!-- this is a comment --> | |
663 | |
664 However, such comments get passed through the processing pipeline and are by | |
665 default included in the final output. If that's not desired, prefix the comment | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
666 text with an exclamation mark: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
667 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
668 .. code-block:: genshi |
226 | 669 |
670 <!-- !this is a comment too, but one that will be stripped from the output --> | |
671 | |
672 Note that it does not matter whether there's whitespace before or after the | |
510
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
673 exclamation mark, so the above could also be written as follows: |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
674 |
ca7d707d51b0
Use syntax highlighting on all the other doc pages, too.
cmlenz
parents:
451
diff
changeset
|
675 .. code-block:: genshi |
226 | 676 |
677 <!--! this is a comment too, but one that will be stripped from the output --> |