Simple Templates In Python
What if you need a very fast way to seperate code and templates? Just for a small CGI or a code generation script? the string.Template Module doesn't provide loops or stuff like that, the same limitation affects the modulo operator of strings and a full template engine is too much for your project? Then try stpy :D
Basically it's a port of the PHP way to create templates for python. The only change: whereas you outdent in python just use the "end" keyword. The engine automatically outdents "elif", "else" and "except".
Here the module: stpy.py.
Excluding docstrings the whole program code is about 80 lines long. Not much for a simple template engine.
You can then use it like this:
from stpy import Template
tmpl = Template('''\
<h1>Template Test</h1>
<ul>
<? for entry in entries: ?>
<li><?= entry['title'] ?> - <?= entry['text'] ?></li>
<? end ?>
</ul>
<h3>Variables defined:</h3>
<pre><?= dir() ?></pre>
''')
print tmpl.render(
entries = [
dict(
title='Test',
text='Hihi'
),
dict(
title='Foobar',
text='blub'
)
]
)
Code is licensed under the BSD License. Have fun :D