Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old August 2nd, 2006, 10:55 AM
Paolo Pantaleo
Guest
 
Posts: n/a
Default Behavior on non definded name in Cheetah

[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

--
if you have a minute to spend please visit my photogrphy site:
http://mypic.co.nr
  #2  
Old August 2nd, 2006, 10:55 AM
Stephan Diehl
Guest
 
Posts: n/a
Default Re: Behavior on non definded name in Cheetah

Paolo Pantaleo wrote:
Quote:
[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/user...r.missing.html
  #3  
Old August 2nd, 2006, 12:05 PM
Paolo Pantaleo
Guest
 
Posts: n/a
Default Re: Behavior on non definded name in Cheetah

2006/8/2, Stephan Diehl <stephan.diehl@gmx.net>:
Quote:
Paolo Pantaleo wrote:
Quote:
[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/user...r.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.

PAolo

--
if you have a minute to spend please visit my photogrphy site:
http://mypic.co.nr
  #4  
Old August 2nd, 2006, 12:35 PM
Peter Otten
Guest
 
Posts: n/a
Default Re: Behavior on non definded name in Cheetah

Paolo Pantaleo wrote:
Quote:
2006/8/2, Stephan Diehl <stephan.diehl@gmx.net>:
Quote:
>Paolo Pantaleo wrote:
Quote:
[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/user...r.missing.html
Quote:
>
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

  #5  
Old August 2nd, 2006, 04:25 PM
Paolo Pantaleo
Guest
 
Posts: n/a
Default Re: Behavior on non definded name in Cheetah

2006/8/2, Peter Otten <__peter__@web.de>:
Quote:
Paolo Pantaleo wrote:
>
Quote:
2006/8/2, Stephan Diehl <stephan.diehl@gmx.net>:
Quote:
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/user...r.missing.html
Quote:
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
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles