RE: [Cheetahtemplate-discuss] Overriding the NotFound exception
Brought to you by:
rtyler,
tavis_rudd
From: Brian B. <bri...@se...> - 2004-08-31 08:31:05
|
Thanks - this is the solution I ended up using (I basically added an instance of SafeDict to the end of the searchList so I didn't need an errorCatcher): ie. t=3DTemplate.Template(source=3D"Start $key $missingkey End",searchList=3Dnamespaces+[SafeDict()]) This works fine, but I just get the feeling there should be a built-in = way to do this (maybe Template.py should have an optional default argument, which if not specified will raise the NotFound exception as normal?) Brian -----Original Message----- From: Doug Wyatt [mailto:do...@so...]=20 Sent: 27 August 2004 18:02 To: Brian Bird Cc: che...@li... Subject: Re: [Cheetahtemplate-discuss] Overriding the NotFound exception What happens if you put an instance of something like this in your=20 search list: class SafeDict(dict): def __getitem__(self, k): if not self.has_key(k): return "" return dict.__getitem__(self, k) ? Actually it probably needs to override more dict methods like hasitem=20 but it seems worth an experiment... Doug On Aug 27, 2004, at 8:40, 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=20 > 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?), but when I pass one to=20 > the template constructor I get an error. Here's the code: > > =A0from Cheetah import ErrorCatchers, Template > =A0namespaces=3D[{"key":"value"}] > =A0t=3DTemplate.Template(source=3D"Start $key $missingkey End", > searchList=3Dnamespaces,errorCatcher=3DErrorCatchers.Echo) > =A0print 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. > > > > Thanks for any help > > Brian |