Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Looking for very light weight template library (not framework) (http://www.velocityreviews.com/forums/t596666-looking-for-very-light-weight-template-library-not-framework.html)

Malcolm Greene 03-07-2008 01:56 AM

Looking for very light weight template library (not framework)
 
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

Erik Max Francis 03-07-2008 02:43 AM

Re: Looking for very light weight template library (not framework)
 
Malcolm Greene wrote:

> 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:

>>> import em, time
>>> 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()
.... """
>>>
>>> class Invoice: pass

....
>>> invoice = Invoice()
>>> invoice.total = "$123.45"
>>> invoice.contact = "Jack McCoy"
>>> invoice.address = "1 Police Plaza\nNew York City, NY"
>>> print em.expand(myOutput, globals())


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

--
Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
There's a reason why we / Keep chasing morning
-- Sandra St. Victor

Carl Banks 03-07-2008 03:39 AM

Re: Looking for very light weight template library (not framework)
 
On Mar 6, 8:56 pm, "Malcolm Greene" <pyt...@bdurham.com> wrote:
> 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

Jeff McNeil 03-07-2008 03:41 AM

Re: Looking for very light weight template library (not framework)
 
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.


On 3/6/08, Erik Max Francis <max@alcyone.com> wrote:
> Malcolm Greene wrote:
>
> > 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:
>
> >>> import em, time

>
> >>> 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()
> ... """
> >>>
> >>> class Invoice: pass

> ...
> >>> invoice = Invoice()
> >>> invoice.total = "$123.45"
> >>> invoice.contact = "Jack McCoy"
> >>> invoice.address = "1 Police Plaza\nNew York City, NY"
> >>> print em.expand(myOutput, globals())

>
> 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
>
>
> --
> Erik Max Francis && max@alcyone.com && http://www.alcyone.com/max/
> San Jose, CA, USA && 37 18 N 121 57 W && AIM, Y!M erikmaxfrancis
> There's a reason why we / Keep chasing morning
> -- Sandra St. Victor
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>


Bernard 03-07-2008 04:37 AM

Re: Looking for very light weight template library (not framework)
 
Cheetah! Cheetah! Cheetah!

On 6 mar, 20:56, "Malcolm Greene" <pyt...@bdurham.com> wrote:
> 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



Duncan Booth 03-07-2008 08:46 AM

Re: Looking for very light weight template library (not framework)
 
"Malcolm Greene" <python@bdurham.com> wrote:

> 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:

>>> from string import Template
>>> class EvalTemplate(Template):

idpattern = '[^{}]+'


>>> class EvalDict(dict):

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


>>> class Invoice:

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


>>> i = Invoice(42)
>>> template = EvalTemplate("The total cost is ${invoice.total}")
>>> template.substitute(EvalDict(invoice=i))

'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.

Roman Bertle 03-07-2008 10:04 AM

Re: Looking for very light weight template library (not framework)
 
* Malcolm Greene <python@bdurham.com>:
> 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/Coo.../Recipe/52305A
or YAPTOO
http://aspn.activestate.com/ASPN/Coo.../Recipe/465508,
very small but powerful templating engines. I use YAPTU myself for
invoice templating.

Regards, Roman

Stefan Behnel 03-07-2008 10:21 AM

Re: Looking for very light weight template library (not framework)
 
Jeff McNeil wrote:
> Isn't there some long
> running joke about new Python programmers creating their own template
> language or something, too? =)


http://article.gmane.org/gmane.comp....general/544922

Stefan


All times are GMT. The time now is 12:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.