Looking for very light weight template library (not framework)

M

Malcolm Greene

New to Python and looking for a template library that allows Python
expressions embedded in strings to be evaluated in place. In other words
something more powerful than the basic "%(variable)s" or "$variable"
(Template) capabilities.

I know that some of the web frameworks support this type of template
capability but I don't need a web framework, just a library that
supports embedded expression evaluation.

Use case:

myOutput = """\

The total cost is {{invoice.total}}.

This order will be shipped to {{invoice.contact}} at the following
address:

{{invoice.address}}

This order was generated at {{some date/time expression}}
"""

Any suggestions appreciated.

Malcolm
 
E

Erik Max Francis

Malcolm said:
New to Python and looking for a template library that allows Python
expressions embedded in strings to be evaluated in place. In other words
something more powerful than the basic "%(variable)s" or "$variable"
(Template) capabilities.

I know that some of the web frameworks support this type of template
capability but I don't need a web framework, just a library that
supports embedded expression evaluation. ...
Any suggestions appreciated.

EmPy may work:

http://www.alcyone.com/software/empy/

Your template would look something like:

myOutput = """\

The total cost is @invoice.total.

This order will be shipped to @invoice.contact at the following
address:

@invoice.address

This order was generated at @time.ctime()
"""

This could be instrumented with something as simple as:
....
.... The total cost is @invoice.total.
....
.... This order will be shipped to @invoice.contact at the following
.... address:
....
.... @invoice.address
....
.... This order was generated at @time.ctime()
.... """
The total cost is $123.45.

This order will be shipped to Jack McCoy at the following
address:

1 Police Plaza
New York City, NY

This order was generated at Thu Mar 6 18:41:58 2008
 
C

Carl Banks

New to Python and looking for a template library that allows Python
expressions embedded in strings to be evaluated in place. In other words
something more powerful than the basic "%(variable)s" or "$variable"
(Template) capabilities.

I know that some of the web frameworks support this type of template
capability but I don't need a web framework, just a library that
supports embedded expression evaluation.

Use case:

myOutput = """\

The total cost is {{invoice.total}}.

This order will be shipped to {{invoice.contact}} at the following
address:

{{invoice.address}}

This order was generated at {{some date/time expression}}
"""

Any suggestions appreciated.


You could do something like this with a single function. For
instance:


import re

def apply_template(text,env):
def eval_in_env(m):
return str(eval(m.group(1),env))
return re.sub(r"{{(.*?)}}",eval_in_env,text)


Where env is a dict with the variables you want to evaluate (locals()
could be a good choice for this).

Standard warning about usage of eval: don't use this with untrusted
input or Bad Things can happen.


Carl Banks
 
J

Jeff McNeil

Cheetah (http://www.cheetahtemplate.org/) and Mako
(http://www.makotemplates.org/) come to mind. Isn't there some long
running joke about new Python programmers creating their own template
language or something, too? =)

I know you said you don't want a web framework, but I've always been a
fan of Django templates in that they don't really allow you to do too
much inside the template itself. That seems to help keep a lot of
ex-PHP/JSP/ASP programmers I know honest.
 
D

Duncan Booth

Malcolm Greene said:
New to Python and looking for a template library that allows Python
expressions embedded in strings to be evaluated in place. In other words
something more powerful than the basic "%(variable)s" or "$variable"
(Template) capabilities.

I know that some of the web frameworks support this type of template
capability but I don't need a web framework, just a library that
supports embedded expression evaluation.

You could try using the Template class:
idpattern = '[^{}]+'

def __getitem__(self, name):
if name in self:
return dict.__getitem__(self, name)
return eval(name, globals(), self)

def __init__(self, total):
self.total = total

'The total cost is 42'

The usual caveats about using 'eval' apply (maybe it should be called
EvilDict), and I'm sure you could override substitute/safe_substitute to
construct the EvalDict transparently.
 
R

Roman Bertle

* Malcolm Greene said:
New to Python and looking for a template library that allows Python
expressions embedded in strings to be evaluated in place. In other words
something more powerful than the basic "%(variable)s" or "$variable"
(Template) capabilities. [...]

Use case:

myOutput = """\

The total cost is {{invoice.total}}.
[...]

You might look at YAPTU
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52305A
or YAPTOO
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/465508,
very small but powerful templating engines. I use YAPTU myself for
invoice templating.

Regards, Roman
 

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

No members online now.

Forum statistics

Threads
473,999
Messages
2,570,246
Members
46,843
Latest member
WizcraftEntertainmentAgen

Latest Threads

Top