Behavior on non definded name in Cheetah

Paolo Pantaleo paolopantaleo at gmail.com
Wed Aug 2 11:31:05 EDT 2006


2006/8/2, Peter Otten <__peter__ at web.de>:
> Paolo Pantaleo wrote:
>
> > 2006/8/2, Stephan Diehl <stephan.diehl at gmx.net>:
> >> Paolo Pantaleo wrote:
> >> > [I hope I am posting to the right place]
> >> >
> >> > I have a cheetah template something like this:
> >> >
> >> > x is: $x
> >> > y is: $y
> >> > z is: $z
> >> >
> >> > [Actually more complicated]
> >> >
> >> > If for example $y is not defined I get an exception and  the parsing
> >> > of the template stops. Is  there any way to substitute $y with an emty
> >> > string and making cheeta going on with parsing?
> >> >
> >> > Thnx
> >> > PAolo
> >> >
> >>
> >>
> http://cheetahtemplate.org/docs/users_guide_html_multipage/language.namemapper.missing.html
> >> --
> >> http://mail.python.org/mailman/listinfo/python-list
> >>
> >
> > Actually I wanted to keep things simple for who writes the template,
> > so I am using this workaround: I define a class
> >
> > class ClassMapper:
> >     def __init__(self,dict={}):
> >         self.__dict=dict
> >     def getValue(self,str):
> >         try:
> >             return self.__dict[str]
> >         except KeyError:
> >             return ""
> >
> > x=ClassMapper(dict)
> > Template(definition, searchList=[{"info":x.getValue}])
> >
> >
> >
> > so the user should do
> >
> > $info("name")
> >
> > Maybe I could define a class that implements a dictionary and doesn''t
> > raise an exception for a key not present... but it seems to
> > complicated.
>
> You mean something like
>
> from Cheetah.Template import Template
>
> class Dict(dict):
>     def __getitem__(self, key):
>         return self.get(key, "")
>
> template = """\
> x is $x
> y is $y
> z is $z
> """
> print Template(template, searchList=[Dict(x="x", y="y")])
>
> You can also make a debugging version:
>
> class Dict(dict):
>     def __getitem__(self, key):
>         return self.get(key, "#missing key: %r#" % key)
>
> Peter
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
Wonderful, thnx a lot. Well not so complicated if you know how to do :D

PAolo



More information about the Python-list mailing list