Mercurial > genshi > genshi-test
comparison doc/templates.txt @ 606:9ada030ad986
Changed the default error handling mode to "strict".
author | cmlenz |
---|---|
date | Mon, 27 Aug 2007 20:05:31 +0000 |
parents | bc5faca93699 |
children | 237050080827 |
comparison
equal
deleted
inserted
replaced
605:bc5faca93699 | 606:9ada030ad986 |
---|---|
229 <b>Hello, world!</b> | 229 <b>Hello, world!</b> |
230 </div> | 230 </div> |
231 | 231 |
232 Code blocks can import modules, define classes and functions, and basically do | 232 Code blocks can import modules, define classes and functions, and basically do |
233 anything you can do in normal Python code. What code blocks can *not* do is to | 233 anything you can do in normal Python code. What code blocks can *not* do is to |
234 produce content that is included directly in the generated page. | 234 produce content that is emitted directly tp the generated output. |
235 | 235 |
236 .. note:: Using the ``print`` statement will print to the standard output | 236 .. note:: Using the ``print`` statement will print to the standard output |
237 stream, just as it does for other Python code in your application. | 237 stream, just as it does for other Python code in your application. |
238 | 238 |
239 Unlike expressions, Python code in ``<?python ?>`` processing instructions can | 239 Unlike expressions, Python code in ``<?python ?>`` processing instructions can |
259 .. _`error handling`: | 259 .. _`error handling`: |
260 | 260 |
261 Error Handling | 261 Error Handling |
262 ============== | 262 ============== |
263 | 263 |
264 By default, Genshi allows you to access variables that are not defined, without | 264 By default, Genshi raises an ``UndefinedError`` if a template expression |
265 raising a ``NameError`` exception as regular Python code would: | 265 attempts to access a variable that is not defined: |
266 | 266 |
267 .. code-block:: pycon | 267 .. code-block:: pycon |
268 | 268 |
269 >>> from genshi.template import MarkupTemplate | 269 >>> from genshi.template import MarkupTemplate |
270 >>> tmpl = MarkupTemplate('<p>${doh}</p>') | 270 >>> tmpl = MarkupTemplate('<p>${doh}</p>') |
271 >>> tmpl.generate().render('xhtml') | |
272 Traceback (most recent call last): | |
273 ... | |
274 UndefinedError: "doh" not defined | |
275 | |
276 You can change this behavior by setting the variable lookup mode to "lenient". | |
277 In that case, accessing undefined variables returns an `Undefined` object, | |
278 meaning that the expression does not fail immediately. See below for details. | |
279 | |
280 If you need to check whether a variable exists in the template context, use the | |
281 defined_ or the value_of_ function described below. To check for existence of | |
282 attributes on an object, or keys in a dictionary, use the ``hasattr()``, | |
283 ``getattr()`` or ``get()`` functions, or the ``in`` operator, just as you would | |
284 in regular Python code: | |
285 | |
286 >>> from genshi.template import MarkupTemplate | |
287 >>> tmpl = MarkupTemplate('<p>${defined("doh")}</p>') | |
288 >>> print tmpl.generate().render('xhtml') | |
289 <p>False</p> | |
290 | |
291 .. note:: Lenient error handling was the default in Genshi prior to version 0.5. | |
292 Strict mode was introduced in version 0.4, and became the default in | |
293 0.5. The reason for this change was that the lenient error handling | |
294 was masking actual errors in templates, thereby also making it harder | |
295 to debug some problems. | |
296 | |
297 | |
298 .. _`lenient`: | |
299 | |
300 Lenient Mode | |
301 ------------ | |
302 | |
303 If you instruct Genshi to use the lenient variable lookup mode, it allows you | |
304 to access variables that are not defined, without raising an ``UndefinedError``. | |
305 | |
306 This mode can be chosen by passing the ``lookup='lenient'`` keyword argument to | |
307 the template initializer, or by passing the ``variable_lookup='lenient'`` | |
308 keyword argument to the ``TemplateLoader`` initializer: | |
309 | |
310 .. code-block:: pycon | |
311 | |
312 >>> from genshi.template import MarkupTemplate | |
313 >>> tmpl = MarkupTemplate('<p>${doh}</p>', lookup='lenient') | |
271 >>> print tmpl.generate().render('xhtml') | 314 >>> print tmpl.generate().render('xhtml') |
272 <p></p> | 315 <p></p> |
273 | 316 |
274 You *will* however get an exception if you try to call an undefined variable, or | 317 You *will* however get an exception if you try to call an undefined variable, or |
275 do anything else with it, such as accessing its attributes: | 318 do anything else with it, such as accessing its attributes: |
276 | 319 |
277 .. code-block:: pycon | 320 .. code-block:: pycon |
278 | 321 |
279 >>> from genshi.template import MarkupTemplate | 322 >>> from genshi.template import MarkupTemplate |
280 >>> tmpl = MarkupTemplate('<p>${doh.oops}</p>') | 323 >>> tmpl = MarkupTemplate('<p>${doh.oops}</p>', lookup='lenient') |
281 >>> print tmpl.generate().render('xhtml') | 324 >>> print tmpl.generate().render('xhtml') |
282 Traceback (most recent call last): | 325 Traceback (most recent call last): |
283 ... | 326 ... |
284 UndefinedError: "doh" not defined | 327 UndefinedError: "doh" not defined |
285 | 328 |
287 against the ``Undefined`` class, for example in a conditional directive: | 330 against the ``Undefined`` class, for example in a conditional directive: |
288 | 331 |
289 .. code-block:: pycon | 332 .. code-block:: pycon |
290 | 333 |
291 >>> from genshi.template import MarkupTemplate | 334 >>> from genshi.template import MarkupTemplate |
292 >>> tmpl = MarkupTemplate('<p>${type(doh) is not Undefined}</p>') | 335 >>> tmpl = MarkupTemplate('<p>${type(doh) is not Undefined}</p>', |
336 ... lookup='lenient') | |
293 >>> print tmpl.generate().render('xhtml') | 337 >>> print tmpl.generate().render('xhtml') |
294 <p>False</p> | 338 <p>False</p> |
295 | 339 |
296 Alternatively, the built-in functions defined_ or value_of_ can be used in this | 340 Alternatively, the built-in functions defined_ or value_of_ can be used in this |
297 case. | 341 case. |
298 | |
299 Strict Mode | |
300 ----------- | |
301 | |
302 In addition to the default "lenient" error handling, Genshi lets you use a less | |
303 forgiving mode if you prefer errors blowing up loudly instead of being ignored | |
304 silently. | |
305 | |
306 This mode can be chosen by passing the ``lookup='strict'`` keyword argument to | |
307 the template initializer, or by passing the ``variable_lookup='strict'`` keyword | |
308 argument to the ``TemplateLoader`` initializer: | |
309 | |
310 .. code-block:: pycon | |
311 | |
312 >>> from genshi.template import MarkupTemplate | |
313 >>> tmpl = MarkupTemplate('<p>${doh}</p>', lookup='strict') | |
314 >>> print tmpl.generate().render('xhtml') | |
315 Traceback (most recent call last): | |
316 ... | |
317 UndefinedError: "doh" not defined | |
318 | |
319 When using strict mode, any reference to an undefined variable, as well as | |
320 trying to access an non-existing item or attribute of an object, will cause an | |
321 ``UndefinedError`` to be raised immediately. | |
322 | |
323 .. note:: While this mode is currently not the default, it may be promoted to | |
324 the default in future versions of Genshi. In general, the default | |
325 lenient error handling mode can be considered dangerous as it silently | |
326 ignores typos. | |
327 | 342 |
328 Custom Modes | 343 Custom Modes |
329 ------------ | 344 ------------ |
330 | 345 |
331 In addition to the built-in "lenient" and "strict" modes, it is also possible to | 346 In addition to the built-in "lenient" and "strict" modes, it is also possible to |