473,507 Members | 2,476 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
Aug 2 '06 #1
4 1344
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
Aug 2 '06 #2
2006/8/2, Stephan Diehl <st***********@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/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
Aug 2 '06 #3
Paolo Pantaleo wrote:
2006/8/2, Stephan Diehl <st***********@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/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.
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

Aug 2 '06 #4
2006/8/2, Peter Otten <__*******@web.de>:
Paolo Pantaleo wrote:
2006/8/2, Stephan Diehl <st***********@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/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.

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
Aug 2 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
2083
by: Roger Jack | last post by:
I have just finished reading Code Generation In Action which uses Ruby for code generation. I would like to use Python instead. Is Cheetah the best tool to use for templating source code files and...
3
1948
by: Timo Virkkala | last post by:
I tried to install Cheetah (0.9.15) and ran into trouble. When running the test suite, I get 61 failures and 57 errors. Most of the errors are "ImportError: No module named temp", with some...
1
2388
by: Mike Grigorov | last post by:
I am using cheetah with jython 2.1 and I have problem generating templates that contain cyrillic text. Some texts are printed OK and others are not. I searched Jython and cheetah projects mailing...
0
905
by: a | last post by:
cheetah - how do i use .refreshcaceh() in cheetah the doc is skimpy and doesnt tell more than you can use refreshcache with id to refresh a cache my code in html has #cache 30m, id =cachei ...
1
1382
by: chun ping wang | last post by:
I am having trouble using cheetah and getting it to work the way i want (mvc) I have the following file. index.py : # imports BibleController and setting. BibleController.py that includes...
2
1365
by: Sai Krishna M | last post by:
Hi, I have been working for some time developing web pages using python, modpython, cheetah. I find that this method has come inherent difficulties in it like if we want to generate a single...
0
1184
by: manuel.reil | last post by:
Hello, currently i am developing a very small cms using python and cheetah. very early i have noticed that i was lacking the method to extract/recover the contents (html,text) from the html that...
1
3065
by: brianrpsgt1 | last post by:
Newbie here.... I have been able to successful pull info from a MySQL DB, get the results and output them in an HTML format using Cheetah to the screen using IDLE. I am doing this on a Windows...
6
3176
by: John Salerno | last post by:
I always have the desire to learn one thing well instead of split my attention between several options, so I'm trying to decide which of these two to start learning. Are there any particular things...
0
7114
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7321
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7034
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5623
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
1544
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
412
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.