Loading ...
Sorry, an error occurred while loading the content.

RE: [Cheetahtemplate-discuss] Overriding the NotFound exception

Expand Messages
  • Brian Bird
    Just to follow up on this bug: I ve written some tests to illustrate the problem. Attached is an updated Template.py from the Cheetah/Tests subdirectory.
    Message 1 of 7 , Aug 31, 2004
      Just to follow up on this bug:
      I've written some tests to illustrate the problem. Attached is an updated
      Template.py from the Cheetah/Tests subdirectory.

      (Sorry, there were no tests in it beforehand so I hope I haven't deviated
      from convention too much)

      Any help appreciated


      -----Original Message-----
      From: cheetahtemplate-discuss-admin@...
      [mailto:cheetahtemplate-discuss-admin@...] On Behalf Of
      Brian Bird
      Sent: 31 August 2004 12:38
      To: mso@...; 'Cheetah Template List'
      Subject: RE: [Cheetahtemplate-discuss] Overriding the NotFound exception

      I thought I'd solved my problem by using a dictionary in the namespace which
      returns "" instead of raising NotFound if a key isn't available (thanks Doug
      for the idea). However, this then means that any inherited methods in my
      templates aren't found (It appears that the namespace is searched before any
      inherited methods - is this right? It seems odd since a value a user submits
      could override a method).

      In my case I can't guarantee I know all the field names being submitted so I
      can't set all the relevant fields to "" before creating the Template.

      So I've re-vistited the errorCatcher solution. If I use #errorCatcher in all
      my templates then it seems to work ok, except that means editing hundreds of
      templates and adding this instruction. I also don't like this idea because
      other users can edit the templates and may accidentally remove this line
      without realising it's importance.

      So (simplified) my example now looks like this:

      class BlankError(ErrorCatchers.ErrorCatcher):
      def warn(self, exc_val, code, rawCode, lineCol):
      return ""
      t=Template.Template(filter=filter, source=source, searchList=[namespace],

      This gives an error because the Template constructor expects a class as the
      errorCatcher argument (line 128), but then later (line 202) it tries to
      access .__class__.__name__ which fails. Removing the __class__ bit stops any
      errors but then the compiler doesn't appear to use the errorCatcher I've

      To fix this I added another line just before the compiler.compile() in the
      compile method (line 245). However, this will cause an error for anyone not
      using an error handler. Also, I can't make this dependant on whether
      self._errorCatcher has been set because this compile() method is called
      before self._errorCatcher is set!

      I'm using the latest 0.9.15 release from sourceforge. Does anyone who knows
      more than me about this code think they could fix it? I can't help thinking
      there should be a simple way to do this but I can't see it...


      -----Original Message-----
      From: Mike Orr [mailto:mso@...]
      Sent: 27 August 2004 19:18
      To: Brian Bird; Cheetah Template List; Tavis Rudd
      Subject: Re: [Cheetahtemplate-discuss] Overriding the NotFound exception

      On Fri, Aug 27, 2004 at 04:40:12PM +0100, Brian Bird wrote:
      > I'd like to change the behaviour of my Template objects so that if a
      > $placeholder variable doesn't exist then a blank string is printed
      > instead of a NotFound exception being raised.
      > Is there an easy way to do this without hacking the Template.py source
      > code?
      > I thought I might get it to work using an error handler (although
      > should I really only use this for debugging?),

      Yes, an ErrorCatcher is what you want.

      > but when I pass one to the template
      > constructor I get an error. Here's the code:
      > from Cheetah import ErrorCatchers, Template
      > namespaces=[{"key":"value"}] t=Template.Template(source="Start $key
      > $missingkey End",
      > searchList=namespaces,errorCatcher=ErrorCatchers.Echo)
      > print t
      > I get the error "AttributeError: class ErrorCatcher has no attribute
      > '__class__' "
      > I have to specify it as a class rather than a string because I want to
      > use my own errorcatcher class if I could get it working.

      There must be a bug in the constructor. If you have time to analyze it and
      make a patch, we'll happily put it in. I've only done it via #errorCatcher,
      not the constructor.

      The only problem with error catchers is they're catching an exception, which
      is inefficient (i.e., slow) in Python. But the difference
      shouldn't be noticeable unless you're filling several templates a second.

      -Mike Orr (aka. Sluggo), mso@... (iron@...)
      http://sluggo.kicks-ass.org/ Cxu vi parolas Esperante?

      This SF.Net email is sponsored by BEA Weblogic Workshop
      FREE Java Enterprise J2EE developer tools!
      Get your free copy of BEA WebLogic Workshop 8.1 today.
      Cheetahtemplate-discuss mailing list
    Your message has been successfully submitted and would be delivered to recipients shortly.