Mercurial > genshi > mirror
comparison doc/templates.txt @ 853:f33ecf3c319e trunk
Convert a bunch of print statements to py3k compatible syntax.
author | cmlenz |
---|---|
date | Tue, 10 Nov 2009 21:22:51 +0000 |
parents | c42693ee739d |
children | 5c7454462cd3 |
comparison
equal
deleted
inserted
replaced
852:07f4339fecb0 | 853:f33ecf3c319e |
---|---|
114 .. code-block:: pycon | 114 .. code-block:: pycon |
115 | 115 |
116 >>> from genshi.template import MarkupTemplate | 116 >>> from genshi.template import MarkupTemplate |
117 >>> tmpl = MarkupTemplate('<h1>Hello, $name!</h1>') | 117 >>> tmpl = MarkupTemplate('<h1>Hello, $name!</h1>') |
118 >>> stream = tmpl.generate(name='world') | 118 >>> stream = tmpl.generate(name='world') |
119 >>> print stream.render('xhtml') | 119 >>> print(stream.render('xhtml')) |
120 <h1>Hello, world!</h1> | 120 <h1>Hello, world!</h1> |
121 | 121 |
122 .. note:: See the Serialization_ section of the `Markup Streams`_ page for | 122 .. note:: See the Serialization_ section of the `Markup Streams`_ page for |
123 information on configuring template output options. | 123 information on configuring template output options. |
124 | 124 |
127 .. code-block:: pycon | 127 .. code-block:: pycon |
128 | 128 |
129 >>> from genshi.template import TextTemplate | 129 >>> from genshi.template import TextTemplate |
130 >>> tmpl = TextTemplate('Hello, $name!') | 130 >>> tmpl = TextTemplate('Hello, $name!') |
131 >>> stream = tmpl.generate(name='world') | 131 >>> stream = tmpl.generate(name='world') |
132 >>> print stream | 132 >>> print(stream) |
133 Hello, world! | 133 Hello, world! |
134 | 134 |
135 .. note:: If you want to use text templates, you should consider using the | 135 .. note:: If you want to use text templates, you should consider using the |
136 ``NewTextTemplate`` class instead of simply ``TextTemplate``. See | 136 ``NewTextTemplate`` class instead of simply ``TextTemplate``. See |
137 the `Text Template Language`_ page. | 137 the `Text Template Language`_ page. |
150 | 150 |
151 from genshi.template import TemplateLoader | 151 from genshi.template import TemplateLoader |
152 loader = TemplateLoader([templates_dir1, templates_dir2]) | 152 loader = TemplateLoader([templates_dir1, templates_dir2]) |
153 tmpl = loader.load('test.html') | 153 tmpl = loader.load('test.html') |
154 stream = tmpl.generate(title='Hello, world!') | 154 stream = tmpl.generate(title='Hello, world!') |
155 print stream.render() | 155 print(stream.render()) |
156 | 156 |
157 See the `API documentation <api/index.html>`_ for details on using Genshi via | 157 See the `API documentation <api/index.html>`_ for details on using Genshi via |
158 the Python API. | 158 the Python API. |
159 | 159 |
160 | 160 |
179 | 179 |
180 .. code-block:: pycon | 180 .. code-block:: pycon |
181 | 181 |
182 >>> from genshi.template import MarkupTemplate | 182 >>> from genshi.template import MarkupTemplate |
183 >>> tmpl = MarkupTemplate('<em>${items[0].capitalize()} item</em>') | 183 >>> tmpl = MarkupTemplate('<em>${items[0].capitalize()} item</em>') |
184 >>> print tmpl.generate(items=['first', 'second']) | 184 >>> print(tmpl.generate(items=['first', 'second'])) |
185 <em>First item</em> | 185 <em>First item</em> |
186 | 186 |
187 Expressions support the full power of Python. In addition, it is possible to | 187 Expressions support the full power of Python. In addition, it is possible to |
188 access items in a dictionary using “dotted notation” (i.e. as if they were | 188 access items in a dictionary using “dotted notation” (i.e. as if they were |
189 attributes), and vice-versa (i.e. access attributes as if they were items in a | 189 attributes), and vice-versa (i.e. access attributes as if they were items in a |
191 | 191 |
192 .. code-block:: pycon | 192 .. code-block:: pycon |
193 | 193 |
194 >>> from genshi.template import MarkupTemplate | 194 >>> from genshi.template import MarkupTemplate |
195 >>> tmpl = MarkupTemplate('<em>${dict.foo}</em>') | 195 >>> tmpl = MarkupTemplate('<em>${dict.foo}</em>') |
196 >>> print tmpl.generate(dict={'foo': 'bar'}) | 196 >>> print(tmpl.generate(dict={'foo': 'bar'})) |
197 <em>bar</em> | 197 <em>bar</em> |
198 | 198 |
199 Because there are two ways to access either attributes or items, expressions | 199 Because there are two ways to access either attributes or items, expressions |
200 do not raise the standard ``AttributeError`` or ``IndexError`` exceptions, but | 200 do not raise the standard ``AttributeError`` or ``IndexError`` exceptions, but |
201 rather an exception of the type ``UndefinedError``. The same kind of error is | 201 rather an exception of the type ``UndefinedError``. The same kind of error is |
211 | 211 |
212 .. code-block:: pycon | 212 .. code-block:: pycon |
213 | 213 |
214 >>> from genshi.template import MarkupTemplate | 214 >>> from genshi.template import MarkupTemplate |
215 >>> tmpl = MarkupTemplate('<em>$foo</em>') # Wanted "$foo" as literal output | 215 >>> tmpl = MarkupTemplate('<em>$foo</em>') # Wanted "$foo" as literal output |
216 >>> print tmpl.generate() | 216 >>> print(tmpl.generate()) |
217 Traceback (most recent call last): | 217 Traceback (most recent call last): |
218 ... | 218 ... |
219 UndefinedError: "foo" not defined | 219 UndefinedError: "foo" not defined |
220 >>> tmpl = MarkupTemplate('<em>$$foo</em>') | 220 >>> tmpl = MarkupTemplate('<em>$$foo</em>') |
221 >>> print tmpl.generate() | 221 >>> print(tmpl.generate()) |
222 <em>$foo</em> | 222 <em>$foo</em> |
223 | 223 |
224 But note that this is not necessary if the characters following the dollar sign | 224 But note that this is not necessary if the characters following the dollar sign |
225 do not qualify as an expression. For example, the following needs no escaping: | 225 do not qualify as an expression. For example, the following needs no escaping: |
226 | 226 |
227 .. code-block:: pycon | 227 .. code-block:: pycon |
228 | 228 |
229 >>> tmpl = MarkupTemplate('<script>$(function() {})</script>') | 229 >>> tmpl = MarkupTemplate('<script>$(function() {})</script>') |
230 >>> print tmpl.generate() | 230 >>> print(tmpl.generate()) |
231 <script>$(function() {})</script> | 231 <script>$(function() {})</script> |
232 | 232 |
233 On the other hand, Genshi will always replace two dollar signs in text with a | 233 On the other hand, Genshi will always replace two dollar signs in text with a |
234 single dollar sign, so you'll need to use three dollar signs to get two in the | 234 single dollar sign, so you'll need to use three dollar signs to get two in the |
235 output: | 235 output: |
236 | 236 |
237 .. code-block:: pycon | 237 .. code-block:: pycon |
238 | 238 |
239 >>> tmpl = MarkupTemplate('<script>$$$("div")</script>') | 239 >>> tmpl = MarkupTemplate('<script>$$$("div")</script>') |
240 >>> print tmpl.generate() | 240 >>> print(tmpl.generate()) |
241 <script>$$("div")</script> | 241 <script>$$("div")</script> |
242 | 242 |
243 | 243 |
244 .. _`code blocks`: | 244 .. _`code blocks`: |
245 | 245 |
336 ``getattr()`` or ``get()`` functions, or the ``in`` operator, just as you would | 336 ``getattr()`` or ``get()`` functions, or the ``in`` operator, just as you would |
337 in regular Python code: | 337 in regular Python code: |
338 | 338 |
339 >>> from genshi.template import MarkupTemplate | 339 >>> from genshi.template import MarkupTemplate |
340 >>> tmpl = MarkupTemplate('<p>${defined("doh")}</p>') | 340 >>> tmpl = MarkupTemplate('<p>${defined("doh")}</p>') |
341 >>> print tmpl.generate().render('xhtml') | 341 >>> print(tmpl.generate().render('xhtml')) |
342 <p>False</p> | 342 <p>False</p> |
343 | 343 |
344 .. note:: Lenient error handling was the default in Genshi prior to version 0.5. | 344 .. note:: Lenient error handling was the default in Genshi prior to version 0.5. |
345 Strict mode was introduced in version 0.4, and became the default in | 345 Strict mode was introduced in version 0.4, and became the default in |
346 0.5. The reason for this change was that the lenient error handling | 346 0.5. The reason for this change was that the lenient error handling |
362 | 362 |
363 .. code-block:: pycon | 363 .. code-block:: pycon |
364 | 364 |
365 >>> from genshi.template import MarkupTemplate | 365 >>> from genshi.template import MarkupTemplate |
366 >>> tmpl = MarkupTemplate('<p>${doh}</p>', lookup='lenient') | 366 >>> tmpl = MarkupTemplate('<p>${doh}</p>', lookup='lenient') |
367 >>> print tmpl.generate().render('xhtml') | 367 >>> print(tmpl.generate().render('xhtml')) |
368 <p></p> | 368 <p></p> |
369 | 369 |
370 You *will* however get an exception if you try to call an undefined variable, or | 370 You *will* however get an exception if you try to call an undefined variable, or |
371 do anything else with it, such as accessing its attributes: | 371 do anything else with it, such as accessing its attributes: |
372 | 372 |
373 .. code-block:: pycon | 373 .. code-block:: pycon |
374 | 374 |
375 >>> from genshi.template import MarkupTemplate | 375 >>> from genshi.template import MarkupTemplate |
376 >>> tmpl = MarkupTemplate('<p>${doh.oops}</p>', lookup='lenient') | 376 >>> tmpl = MarkupTemplate('<p>${doh.oops}</p>', lookup='lenient') |
377 >>> print tmpl.generate().render('xhtml') | 377 >>> print(tmpl.generate().render('xhtml')) |
378 Traceback (most recent call last): | 378 Traceback (most recent call last): |
379 ... | 379 ... |
380 UndefinedError: "doh" not defined | 380 UndefinedError: "doh" not defined |
381 | 381 |
382 If you need to know whether a variable is defined, you can check its type | 382 If you need to know whether a variable is defined, you can check its type |
385 .. code-block:: pycon | 385 .. code-block:: pycon |
386 | 386 |
387 >>> from genshi.template import MarkupTemplate | 387 >>> from genshi.template import MarkupTemplate |
388 >>> tmpl = MarkupTemplate('<p>${type(doh) is not Undefined}</p>', | 388 >>> tmpl = MarkupTemplate('<p>${type(doh) is not Undefined}</p>', |
389 ... lookup='lenient') | 389 ... lookup='lenient') |
390 >>> print tmpl.generate().render('xhtml') | 390 >>> print(tmpl.generate().render('xhtml')) |
391 <p>False</p> | 391 <p>False</p> |
392 | 392 |
393 Alternatively, the built-in functions defined_ or value_of_ can be used in this | 393 Alternatively, the built-in functions defined_ or value_of_ can be used in this |
394 case. | 394 case. |
395 | 395 |