ZPT Basics (part 3) - Handling Errors (
Page 5 of 6 )
You can add primitive error-handling to
your templates via the special "on-error" attribute, which allows you to specify
a simple error message that is displayed when TAL encounters an error. Consider
the following example:
<font color="red" size="2" face="verdana"
tal:on-error="string:Form
input error"
tal:attributes="color request/color; size request/size; face
request/face">
Sticks and stones will break my bones </font>
In this case, since my code requires the "size", "face" and "color" input variables
to be specified as input parameters via the URL (notice that I have not used the
"default" condition here, as I did in the previous example), an error will be
generated if these variables are not present. However, rather than displaying
the standard Zope error message,
I can intercept the error with TAL's "on-error" attribute, and replace it with
my own, simpler error message.
"on-error" attributes are processed in a hierarchical manner, with TAL recursing
upwards from the source of the error until it finds an "on-error" error handler.
In the following example, even though the error is generated by the innermost
<font> element, the error message generated comes from the outermost <div>
element.
<div tal:on-error="string:Form input error">
<b><font color="red" size="2"
face="verdana"
tal:attributes="color request/color; size request/size;
face
request/face"> Sticks and stones will break my bones </font> </b> </div>
This is obviously a primitive example - you can do some pretty complex things
with the "on-error" attribute. Take a look at
http://www.zope.org/Wikis/DevSite/Projects/ZPT/RenderErrorHandlingStrategies for more information.