Behavior on non definded name in Cheetah

P

Paolo Pantaleo

[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
 
S

Stephan Diehl

P

Paolo Pantaleo

2006/8/2 said:
Paolo said:
[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

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.

PAolo
 
P

Peter Otten

Paolo said:
2006/8/2 said:
Paolo said:
[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

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
 
P

Paolo Pantaleo

2006/8/2 said:
Paolo said:
2006/8/2 said:
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

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
Wonderful, thnx a lot. Well not so complicated if you know how to do :D

PAolo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top