annotate doc/templates.txt @ 877:8747234921b6

ChangeLog entry for [1099].
author cmlenz
date Thu, 15 Apr 2010 21:08:06 +0000
parents b8ac236f0254
children 5a1c0ee0f659
rev   line source
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:
diff changeset
1 .. -*- mode: rst; encoding: utf-8 -*-
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:
diff changeset
2
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:
diff changeset
3 ========================
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:
diff changeset
4 Genshi Templating Basics
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:
diff changeset
5 ========================
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:
diff changeset
6
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:
diff changeset
7 Genshi provides a template engine that can be used for generating either
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:
diff changeset
8 markup (such as HTML_ or XML_) or plain text. While both share some of the
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:
diff changeset
9 syntax (and much of the underlying implementation) they are essentially
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:
diff changeset
10 separate languages.
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:
diff changeset
11
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:
diff changeset
12 .. _html: http://www.w3.org/html/
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:
diff changeset
13 .. _xml: http://www.w3.org/XML/
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:
diff changeset
14
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:
diff changeset
15 This document describes the common parts of the template engine and will be most
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:
diff changeset
16 useful as reference to those developing Genshi templates. Templates are XML or
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:
diff changeset
17 plain text files that include processing directives_ that affect how the
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:
diff changeset
18 template is rendered, and template expressions_ that are dynamically substituted
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:
diff changeset
19 by variable data.
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:
diff changeset
20
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:
diff changeset
21
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:
diff changeset
22 .. contents:: Contents
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:
diff changeset
23 :depth: 3
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:
diff changeset
24 .. sectnum::
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:
diff changeset
25
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:
diff changeset
26 --------
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:
diff changeset
27 Synopsis
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:
diff changeset
28 --------
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:
diff changeset
29
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:
diff changeset
30 A Genshi *markup template* is a well-formed XML document with embedded Python
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:
diff changeset
31 used for control flow and variable substitution. Markup templates should be
875
b8ac236f0254 Minor doc tweak.
cmlenz
parents: 853
diff changeset
32 used to generate any kind of HTML or XML output, as they provide a number of
b8ac236f0254 Minor doc tweak.
cmlenz
parents: 853
diff changeset
33 advantages over simple text-based templates (such as automatic escaping of
b8ac236f0254 Minor doc tweak.
cmlenz
parents: 853
diff changeset
34 variable data).
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:
diff changeset
35
875
b8ac236f0254 Minor doc tweak.
cmlenz
parents: 853
diff changeset
36 The following is a simple Genshi markup template:
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
37
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
38 .. code-block:: genshi
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:
diff changeset
39
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:
diff changeset
40 <?python
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:
diff changeset
41 title = "A Genshi Template"
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:
diff changeset
42 fruits = ["apple", "orange", "kiwi"]
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:
diff changeset
43 ?>
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:
diff changeset
44 <html xmlns:py="http://genshi.edgewall.org/">
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:
diff changeset
45 <head>
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:
diff changeset
46 <title py:content="title">This is replaced.</title>
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:
diff changeset
47 </head>
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:
diff changeset
48
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:
diff changeset
49 <body>
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:
diff changeset
50 <p>These are some of my favorite fruits:</p>
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:
diff changeset
51 <ul>
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:
diff changeset
52 <li py:for="fruit in fruits">
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:
diff changeset
53 I like ${fruit}s
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:
diff changeset
54 </li>
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:
diff changeset
55 </ul>
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:
diff changeset
56 </body>
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:
diff changeset
57 </html>
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:
diff changeset
58
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:
diff changeset
59 This example shows:
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:
diff changeset
60
875
b8ac236f0254 Minor doc tweak.
cmlenz
parents: 853
diff changeset
61 (a) a Python code block in a processing instruction
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:
diff changeset
62 (b) the Genshi namespace declaration
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:
diff changeset
63 (c) usage of templates directives (``py:content`` and ``py:for``)
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:
diff changeset
64 (d) an inline Python expression (``${fruit}``).
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:
diff changeset
65
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
66 The template would generate output similar to this:
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
67
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
68 .. code-block:: genshi
445
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
69
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
70 <html>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
71 <head>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
72 <title>A Genshi Template</title>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
73 </head>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
74
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
75 <body>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
76 <p>These are some of my favorite fruits:</p>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
77 <ul>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
78 <li>I like apples</li>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
79 <li>I like oranges</li>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
80 <li>I like kiwis</li>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
81 </ul>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
82 </body>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
83 </html>
906b346513b6 Add documentation page on the plugin API.
cmlenz
parents: 442
diff changeset
84
875
b8ac236f0254 Minor doc tweak.
cmlenz
parents: 853
diff changeset
85 A *text template* is a simple plain text document that can also contain
b8ac236f0254 Minor doc tweak.
cmlenz
parents: 853
diff changeset
86 embedded Python code. Text templates are intended to be used for simple
b8ac236f0254 Minor doc tweak.
cmlenz
parents: 853
diff changeset
87 *non-markup* text formats, such as the body of an plain text email. For
b8ac236f0254 Minor doc tweak.
cmlenz
parents: 853
diff changeset
88 example:
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
89
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
90 .. code-block:: genshitext
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:
diff changeset
91
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:
diff changeset
92 Dear $name,
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:
diff changeset
93
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:
diff changeset
94 These are some of my favorite fruits:
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:
diff changeset
95 #for fruit in fruits
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:
diff changeset
96 * $fruit
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:
diff changeset
97 #end
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:
diff changeset
98
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:
diff changeset
99
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:
diff changeset
100 ----------
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:
diff changeset
101 Python API
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:
diff changeset
102 ----------
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:
diff changeset
103
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:
diff changeset
104 The Python code required for templating with Genshi is generally based on the
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:
diff changeset
105 following pattern:
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:
diff changeset
106
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:
diff changeset
107 * Attain a ``MarkupTemplate`` or ``TextTemplate`` object from a string or
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:
diff changeset
108 file-like object containing the template source. This can either be done
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:
diff changeset
109 directly, or through a ``TemplateLoader`` instance.
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:
diff changeset
110 * Call the ``generate()`` method of the template, passing any data that should
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:
diff changeset
111 be made available to the template as keyword arguments.
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:
diff changeset
112 * Serialize the resulting stream using its ``render()`` method.
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:
diff changeset
113
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
114 For example:
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
115
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
116 .. code-block:: pycon
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:
diff changeset
117
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:
diff changeset
118 >>> from genshi.template import MarkupTemplate
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:
diff changeset
119 >>> tmpl = MarkupTemplate('<h1>Hello, $name!</h1>')
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:
diff changeset
120 >>> stream = tmpl.generate(name='world')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
121 >>> print(stream.render('xhtml'))
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:
diff changeset
122 <h1>Hello, world!</h1>
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:
diff changeset
123
592
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
124 .. note:: See the Serialization_ section of the `Markup Streams`_ page for
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
125 information on configuring template output options.
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
126
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
127 Using a text template is similar:
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
128
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
129 .. code-block:: pycon
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:
diff changeset
130
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:
diff changeset
131 >>> from genshi.template import TextTemplate
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:
diff changeset
132 >>> tmpl = TextTemplate('Hello, $name!')
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:
diff changeset
133 >>> stream = tmpl.generate(name='world')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
134 >>> print(stream)
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:
diff changeset
135 Hello, world!
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:
diff changeset
136
592
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
137 .. note:: If you want to use text templates, you should consider using the
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
138 ``NewTextTemplate`` class instead of simply ``TextTemplate``. See
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
139 the `Text Template Language`_ page.
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:
diff changeset
140
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:
diff changeset
141 .. _serialization: streams.html#serialization
592
7145e4eba2ec Add a new syntax for text templates, which is available alongside the old syntax for now. The new syntax is more poweful and flexible, using Django-style directive notation.
cmlenz
parents: 545
diff changeset
142 .. _`Text Template Language`: text-templates.html
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:
diff changeset
143 .. _`Markup Streams`: streams.html
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:
diff changeset
144
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:
diff changeset
145 Using a template loader provides the advantage that “compiled” templates are
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:
diff changeset
146 automatically cached, and only parsed again when the template file changes. In
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:
diff changeset
147 addition, it enables the use of a *template search path*, allowing template
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:
diff changeset
148 directories to be spread across different file-system locations. Using a
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
149 template loader would generally look as follows:
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
150
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
151 .. code-block:: python
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:
diff changeset
152
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:
diff changeset
153 from genshi.template import TemplateLoader
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:
diff changeset
154 loader = TemplateLoader([templates_dir1, templates_dir2])
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:
diff changeset
155 tmpl = loader.load('test.html')
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:
diff changeset
156 stream = tmpl.generate(title='Hello, world!')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
157 print(stream.render())
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:
diff changeset
158
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:
diff changeset
159 See the `API documentation <api/index.html>`_ for details on using Genshi via
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:
diff changeset
160 the Python API.
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:
diff changeset
161
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:
diff changeset
162
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:
diff changeset
163 .. _`expressions`:
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:
diff changeset
164
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:
diff changeset
165 ------------------------------------
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:
diff changeset
166 Template Expressions and Code Blocks
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:
diff changeset
167 ------------------------------------
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:
diff changeset
168
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:
diff changeset
169 Python_ expressions can be used in text and directive arguments. An expression
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:
diff changeset
170 is substituted with the result of its evaluation against the template data.
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:
diff changeset
171 Expressions in text (which includes the values of non-directive attributes) need
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:
diff changeset
172 to prefixed with a dollar sign (``$``) and usually enclosed in curly braces
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:
diff changeset
173 (``{…}``).
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:
diff changeset
174
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:
diff changeset
175 .. _python: http://www.python.org/
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:
diff changeset
176
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:
diff changeset
177 If the expression starts with a letter and contains only letters, digits, dots,
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:
diff changeset
178 and underscores, the curly braces may be omitted. In all other cases, the
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:
diff changeset
179 braces are required so that the template processor knows where the expression
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
180 ends:
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
181
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
182 .. code-block:: pycon
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:
diff changeset
183
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:
diff changeset
184 >>> from genshi.template import MarkupTemplate
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:
diff changeset
185 >>> tmpl = MarkupTemplate('<em>${items[0].capitalize()} item</em>')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
186 >>> print(tmpl.generate(items=['first', 'second']))
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:
diff changeset
187 <em>First item</em>
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:
diff changeset
188
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:
diff changeset
189 Expressions support the full power of Python. In addition, it is possible to
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:
diff changeset
190 access items in a dictionary using “dotted notation” (i.e. as if they were
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:
diff changeset
191 attributes), and vice-versa (i.e. access attributes as if they were items in a
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
192 dictionary):
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
193
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
194 .. code-block:: pycon
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:
diff changeset
195
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:
diff changeset
196 >>> from genshi.template import MarkupTemplate
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:
diff changeset
197 >>> tmpl = MarkupTemplate('<em>${dict.foo}</em>')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
198 >>> print(tmpl.generate(dict={'foo': 'bar'}))
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:
diff changeset
199 <em>bar</em>
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:
diff changeset
200
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:
diff changeset
201 Because there are two ways to access either attributes or items, expressions
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:
diff changeset
202 do not raise the standard ``AttributeError`` or ``IndexError`` exceptions, but
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:
diff changeset
203 rather an exception of the type ``UndefinedError``. The same kind of error is
476
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
204 raised when you try to use a top-level variable that is not in the context data.
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
205 See `Error Handling`_ below for details on how such errors are handled.
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:
diff changeset
206
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:
diff changeset
207
811
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
208 Escaping
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
209 ========
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
210
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
211 If you need to include a literal dollar sign in the output where Genshi would
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
212 normally detect an expression, you can simply add another dollar sign:
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
213
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
214 .. code-block:: pycon
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
215
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
216 >>> from genshi.template import MarkupTemplate
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
217 >>> tmpl = MarkupTemplate('<em>$foo</em>') # Wanted "$foo" as literal output
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
218 >>> print(tmpl.generate())
811
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
219 Traceback (most recent call last):
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
220 ...
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
221 UndefinedError: "foo" not defined
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
222 >>> tmpl = MarkupTemplate('<em>$$foo</em>')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
223 >>> print(tmpl.generate())
811
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
224 <em>$foo</em>
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
225
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
226 But note that this is not necessary if the characters following the dollar sign
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
227 do not qualify as an expression. For example, the following needs no escaping:
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
228
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
229 .. code-block:: pycon
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
230
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
231 >>> tmpl = MarkupTemplate('<script>$(function() {})</script>')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
232 >>> print(tmpl.generate())
811
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
233 <script>$(function() {})</script>
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
234
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
235 On the other hand, Genshi will always replace two dollar signs in text with a
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
236 single dollar sign, so you'll need to use three dollar signs to get two in the
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
237 output:
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
238
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
239 .. code-block:: pycon
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
240
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
241 >>> tmpl = MarkupTemplate('<script>$$$("div")</script>')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
242 >>> print(tmpl.generate())
811
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
243 <script>$$("div")</script>
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
244
b1e9e3209c6f Add doc section on expression escaping. Closes #282.
cmlenz
parents: 756
diff changeset
245
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:
diff changeset
246 .. _`code blocks`:
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:
diff changeset
247
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:
diff changeset
248 Code Blocks
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:
diff changeset
249 ===========
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:
diff changeset
250
609
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
251 Templates also support full Python code blocks, using the ``<?python ?>``
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
252 processing instruction in XML templates:
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
253
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
254 .. code-block:: genshi
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:
diff changeset
255
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:
diff changeset
256 <div>
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:
diff changeset
257 <?python
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:
diff changeset
258 from genshi.builder import tag
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:
diff changeset
259 def greeting(name):
649
523a706d171e Fix typo in docs. Closes #153.
cmlenz
parents: 609
diff changeset
260 return tag.b('Hello, %s!' % name) ?>
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:
diff changeset
261 ${greeting('world')}
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:
diff changeset
262 </div>
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:
diff changeset
263
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
264 This will produce the following output:
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
265
609
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
266 .. code-block:: xml
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:
diff changeset
267
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:
diff changeset
268 <div>
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:
diff changeset
269 <b>Hello, world!</b>
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:
diff changeset
270 </div>
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:
diff changeset
271
609
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
272 In text templates (although only those using the new syntax introduced in
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
273 Genshi 0.5), code blocks use the special ``{% python %}`` directive:
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
274
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
275 .. code-block:: genshitext
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
276
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
277 {% python
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
278 from genshi.builder import tag
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
279 def greeting(name):
649
523a706d171e Fix typo in docs. Closes #153.
cmlenz
parents: 609
diff changeset
280 return 'Hello, %s!' % name
609
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
281 %}
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
282 ${greeting('world')}
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
283
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
284 This will produce the following output::
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
285
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
286 Hello, world!
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
287
237050080827 Add support for Python code blocks in text templates using the new syntax.
cmlenz
parents: 606
diff changeset
288
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:
diff changeset
289 Code blocks can import modules, define classes and functions, and basically do
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:
diff changeset
290 anything you can do in normal Python code. What code blocks can *not* do is to
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
291 produce content that is emitted directly tp the generated output.
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:
diff changeset
292
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:
diff changeset
293 .. note:: Using the ``print`` statement will print to the standard output
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:
diff changeset
294 stream, just as it does for other Python code in your application.
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:
diff changeset
295
476
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
296 Unlike expressions, Python code in ``<?python ?>`` processing instructions can
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
297 not use item and attribute access in an interchangeable manner. That means that
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
298 “dotted notation” is always attribute access, and vice-versa.
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
299
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
300 The support for Python code blocks in templates is not supposed to encourage
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
301 mixing application code into templates, which is generally considered bad
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
302 design. If you're using many code blocks, that may be a sign that you should
081c70ff7acd Update documentation to reflect changes in [573].
cmlenz
parents: 445
diff changeset
303 move such code into separate Python modules.
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:
diff changeset
304
545
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 510
diff changeset
305 If you'd rather not allow the use of Python code blocks in templates, you can
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 510
diff changeset
306 simply set the ``allow_exec`` parameter (available on the ``Template`` and the
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 510
diff changeset
307 ``TemplateLoader`` initializers) to ``False``. In that case Genshi will raise
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 510
diff changeset
308 a syntax error when a ``<?python ?>`` processing instruction is encountered.
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 510
diff changeset
309 But please note that disallowing code blocks in templates does not turn Genshi
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 510
diff changeset
310 into a sandboxable template engine; there are sufficient ways to do harm even
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 510
diff changeset
311 using plain expressions.
6e21c89d9255 Support for Python code blocks in templates can now be disabled. Closes #123.
cmlenz
parents: 510
diff changeset
312
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:
diff changeset
313
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:
diff changeset
314 .. _`error handling`:
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:
diff changeset
315
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:
diff changeset
316 Error Handling
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:
diff changeset
317 ==============
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:
diff changeset
318
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
319 By default, Genshi raises an ``UndefinedError`` if a template expression
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
320 attempts to access a variable that is not defined:
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
321
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
322 .. code-block:: pycon
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:
diff changeset
323
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:
diff changeset
324 >>> from genshi.template import MarkupTemplate
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:
diff changeset
325 >>> tmpl = MarkupTemplate('<p>${doh}</p>')
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
326 >>> tmpl.generate().render('xhtml')
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
327 Traceback (most recent call last):
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
328 ...
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
329 UndefinedError: "doh" not defined
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
330
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
331 You can change this behavior by setting the variable lookup mode to "lenient".
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
332 In that case, accessing undefined variables returns an `Undefined` object,
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
333 meaning that the expression does not fail immediately. See below for details.
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
334
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
335 If you need to check whether a variable exists in the template context, use the
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
336 defined_ or the value_of_ function described below. To check for existence of
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
337 attributes on an object, or keys in a dictionary, use the ``hasattr()``,
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
338 ``getattr()`` or ``get()`` functions, or the ``in`` operator, just as you would
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
339 in regular Python code:
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
340
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
341 >>> from genshi.template import MarkupTemplate
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
342 >>> tmpl = MarkupTemplate('<p>${defined("doh")}</p>')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
343 >>> print(tmpl.generate().render('xhtml'))
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
344 <p>False</p>
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
345
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
346 .. note:: Lenient error handling was the default in Genshi prior to version 0.5.
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
347 Strict mode was introduced in version 0.4, and became the default in
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
348 0.5. The reason for this change was that the lenient error handling
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
349 was masking actual errors in templates, thereby also making it harder
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
350 to debug some problems.
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
351
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
352
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
353 .. _`lenient`:
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
354
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
355 Lenient Mode
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
356 ------------
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
357
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
358 If you instruct Genshi to use the lenient variable lookup mode, it allows you
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
359 to access variables that are not defined, without raising an ``UndefinedError``.
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
360
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
361 This mode can be chosen by passing the ``lookup='lenient'`` keyword argument to
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
362 the template initializer, or by passing the ``variable_lookup='lenient'``
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
363 keyword argument to the ``TemplateLoader`` initializer:
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
364
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
365 .. code-block:: pycon
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
366
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
367 >>> from genshi.template import MarkupTemplate
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
368 >>> tmpl = MarkupTemplate('<p>${doh}</p>', lookup='lenient')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
369 >>> print(tmpl.generate().render('xhtml'))
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:
diff changeset
370 <p></p>
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:
diff changeset
371
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:
diff changeset
372 You *will* however get an exception if you try to call an undefined variable, or
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
373 do anything else with it, such as accessing its attributes:
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
374
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
375 .. code-block:: pycon
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:
diff changeset
376
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:
diff changeset
377 >>> from genshi.template import MarkupTemplate
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
378 >>> tmpl = MarkupTemplate('<p>${doh.oops}</p>', lookup='lenient')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
379 >>> print(tmpl.generate().render('xhtml'))
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:
diff changeset
380 Traceback (most recent call last):
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:
diff changeset
381 ...
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:
diff changeset
382 UndefinedError: "doh" not defined
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:
diff changeset
383
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:
diff changeset
384 If you need to know whether a variable is defined, you can check its type
510
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
385 against the ``Undefined`` class, for example in a conditional directive:
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
386
ca7d707d51b0 Use syntax highlighting on all the other doc pages, too.
cmlenz
parents: 476
diff changeset
387 .. code-block:: pycon
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:
diff changeset
388
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:
diff changeset
389 >>> from genshi.template import MarkupTemplate
606
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
390 >>> tmpl = MarkupTemplate('<p>${type(doh) is not Undefined}</p>',
9ada030ad986 Changed the default error handling mode to "strict".
cmlenz
parents: 605
diff changeset
391 ... lookup='lenient')
853
4376010bb97e Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents: 811
diff changeset
392 >>> print(tmpl.generate().render('xhtml'))
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:
diff changeset
393 <p>False</p>
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:
diff changeset
394
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:
diff changeset
395 Alternatively, the built-in functions defined_ or value_of_ can be used in this
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:
diff changeset
396 case.
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:
diff changeset
397
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:
diff changeset
398 Custom Modes
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:
diff changeset
399 ------------
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:
diff changeset
400
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:
diff changeset
401 In addition to the built-in "lenient" and "strict" modes, it is also possible to
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:
diff changeset
402 use a custom error handling mode. For example, you could use lenient error
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:
diff changeset
403 handling in a production environment, while also logging a warning when an
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:
diff changeset
404 undefined variable is referenced.
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:
diff changeset
405
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:
diff changeset
406 See the API documentation of the ``genshi.template.eval`` module for details.
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:
diff changeset
407
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:
diff changeset
408
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:
diff changeset
409 Built-in Functions & Types
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:
diff changeset
410 ==========================
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:
diff changeset
411
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:
diff changeset
412 The following functions and types are available by default in template code, in
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:
diff changeset
413 addition to the standard built-ins that are available to all Python code.
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:
diff changeset
414
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:
diff changeset
415 .. _`defined`:
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:
diff changeset
416
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:
diff changeset
417 ``defined(name)``
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:
diff changeset
418 -----------------
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:
diff changeset
419 This function determines whether a variable of the specified name exists in
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:
diff changeset
420 the context data, and returns ``True`` if it does.
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:
diff changeset
421
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:
diff changeset
422 .. _`value_of`:
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:
diff changeset
423
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:
diff changeset
424 ``value_of(name, default=None)``
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:
diff changeset
425 --------------------------------
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:
diff changeset
426 This function returns the value of the variable with the specified name if
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:
diff changeset
427 such a variable is defined, and returns the value of the ``default``
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:
diff changeset
428 parameter if no such variable is defined.
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:
diff changeset
429
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:
diff changeset
430 .. _`Markup`:
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:
diff changeset
431
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:
diff changeset
432 ``Markup(text)``
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:
diff changeset
433 ----------------
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:
diff changeset
434 The ``Markup`` type marks a given string as being safe for inclusion in markup,
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:
diff changeset
435 meaning it will *not* be escaped in the serialization stage. Use this with care,
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:
diff changeset
436 as not escaping a user-provided string may allow malicious users to open your
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:
diff changeset
437 web site to cross-site scripting attacks.
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:
diff changeset
438
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:
diff changeset
439 .. _`Undefined`:
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:
diff changeset
440
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:
diff changeset
441 ``Undefined``
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:
diff changeset
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:
diff changeset
443 The ``Undefined`` type can be used to check whether a reference variable is
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:
diff changeset
444 defined, as explained in `error handling`_.
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:
diff changeset
445
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:
diff changeset
446
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:
diff changeset
447 .. _`directives`:
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:
diff changeset
448
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:
diff changeset
449 -------------------
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:
diff changeset
450 Template Directives
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:
diff changeset
451 -------------------
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:
diff changeset
452
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:
diff changeset
453 Directives provide control flow functionality for templates, such as conditions
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:
diff changeset
454 or iteration. As the syntax for directives depends on whether you're using
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:
diff changeset
455 markup or text templates, refer to the
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:
diff changeset
456 `XML Template Language <xml-templates.html>`_ or
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:
diff changeset
457 `Text Template Language <text-templates.html>`_ pages for information.
Copyright (C) 2012-2017 Edgewall Software