logo       

Re: the "Cheetah files" (part 1): msg#00021

python.cheetah

Subject: Re: the "Cheetah files" (part 1)

On Mon, Oct 06, 2003 at 11:40:31AM +0200, Stefan Reich?r wrote:
> I tried to use your href function, but the quoteHtml can not be found
> on my system.

from cgi import escape as quoteHtml
from urllib import quote as quoteUrl

Here's a couple other functions too:

def plural(lis, singular, plural):
"""Usage: $plural('dog', 'dogs')"""
if len(lis) == 1:
return singular
else:
return plural

def stars(count):
"""Usage: $stars(5) # E.g., to rate movies"""
before = '<FONT COLOR="#ff0000"><STRONG>'
stars = '*' * count
after = '</STRONG></FONT>'
return before + stars + after

> What is the best way to import such functions in a template:
> I tried:
> #from CheetahHelpFunctions import href
> This does work.
>
> However:
> #from CheetahHelpFunctions import *
> does not work - I guess this is a feature?

It works for me in precompiled templates but not in dynamically-compiled
ones. I don't know why, but dynamically-compiled templates also have
other limitations like not being able to #extends classes defined in
your module. That's because the generated class doesn't have access to
your module variables, so it doesn't see your class.

The following program generates the following exception. It shows that
the problem happens in the compile step, it's looking for a literal '*'
defined in the imported module. I guess the compiler handles
precompiled #import vs dynamically-compiled #import in totally different
ways.

from Cheetah.Template import Template
SRC="#from os import *\nLa la la."
t = Template(SRC)
print str(t)

Traceback (most recent call last):
File "/tmp/x.py", line 5, in ?
t = Template(SRC)
File
"/local/opt/Python-2.3/lib/python2.3/site-packages/Cheetah/Template.py",
line 156, in __init__
self.compile(source, file)
File
"/local/opt/Python-2.3/lib/python2.3/site-packages/Cheetah/Template.py",
line 245, in compile
compiler.compile()
File
"/local/opt/Python-2.3/lib/python2.3/site-packages/Cheetah/Compiler.py",
line 1086, in compile
self.parse()
File
"/local/opt/Python-2.3/lib/python2.3/site-packages/Cheetah/Parser.py",
line 1036, in parse
self.eatDirective()
File
"/local/opt/Python-2.3/lib/python2.3/site-packages/Cheetah/Parser.py",
line 1150, in eatDirective
self.directiveEaters[directiveKey]()
File
"/local/opt/Python-2.3/lib/python2.3/site-packages/Cheetah/Parser.py",
line 1424, in eatImport
val = getattr(mod, varName.split('.')[0])
AttributeError: 'module' object has no attribute '*'


But if you import each object explicitly (as I would do anyway), it
works:

from Cheetah.Template import Template
SRC="#from os import sep, extsep\nLa la la: $sep, $extsep"
t = Template(SRC)
print str(t)

% python /tmp/x.py
La la la: /, .

--
-Mike Orr (aka. Sluggo), mso@xxxxxx (iron@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx)
http://iron.cx/ English * Esperanto * Russkiy * Deutsch * Espan~ol


-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise