Thejesh GN
Thejesh GN

Reputation: 1128

light weight template engine for python

Which is the simplest and light weight html templating engine in Python which I can use to generate customized email newsletters.

Upvotes: 12

Views: 17006

Answers (4)

diyism
diyism

Reputation: 12935

Give a try to python-micro-template:

https://github.com/diyism/python-micro-template

Usage example(kivy):

import python_micro_template
...
kvml=open('example_kivy_scrollview.kvml', 'r').read()
kvml=python_micro_template.tpl.parse(kvml)
grid=Builder.load_string(kvml)
...

Template example(kvml):

<:for i in range(30):#{#:>
Button:
    text: '<:=i:><:for j in range(6):#{#:><:=j:><:#}#:>'
    size: 480, 40
    size_hint: None, None
<:#}#:>

Upvotes: -3

pts
pts

Reputation: 87271

Searching for tiny template Python in Google turned up Titen, whose source code is only 5.5 kB. Titen can do iteration over lists, which the built-in str.format can't.

Mako claims to be lightweight, but it's relatively fat (>200 kB) compared to Titen. Jinja2 and Django Templates are also well over 100 kB.

Upvotes: 0

the wolf
the wolf

Reputation: 35532

Anything wrong with string.Template? This is in the standard Python distribution and covered by PEP 292:

from string import Template

form=Template('''Dear $john,

I am sorry to imform you, $john, but you will not be my husband
when you return from the $theater war. So sorry about that. Your
$action has caused me to reconsider.

Yours [NOT!!] forever,

Becky

''')

first={'john':'Joe','theater':'Afgan','action':'love'}
second={'john':'Robert','theater':'Iraq','action':'kiss'}
third={'john':'Jose','theater':'Korean','action':'discussion'}

print form.substitute(first)
print form.substitute(second)
print form.substitute(third)

Upvotes: 21

Rafe Kettler
Rafe Kettler

Reputation: 76955

For a really minor templating task, Python itself isn't that bad. Example:

def dynamic_text(name, food):
    return """
    Dear %(name)s,
    We're glad to hear that you like %(food)s and we'll be sending you some more soon.
    """ % {'name':name, 'food':food}

In this sense, you can use string formatting in Python for simple templating. That's about as lightweight as it gets.

If you want to go a bit deeper, Jinja2 is the most "designer friendly" (read: simple) templating engine in the opinion of many.

You can also look into Mako and Genshi. Ultimately, the choice is yours (what has the features you'd like and integrates nicely with your system).

Upvotes: 20

Related Questions