python template engine
时间:2011-02-22 来源:Mingxx
a fast and full-featured template engine based on embedded Python.
install:
sudo easy_install Tenjin
example:
## views/example.pyhtml<?py #@ARGS title, items ?>
<h2>${title}</h2>
<table>
<?py cycle = new_cycle('odd', 'even') ?>
<?py for item in items: ?>
<tr class="${cycle()}">
<td>${item}</td>
</tr>
<?py #endfor ?>
</table>
## main.py
import tenjin
#tenjin.set_template_encoding('utf-8') # optional (default 'utf-8')
from tenjin.helpers import *
from tenjin.html import *
engine = tenjin.Engine(path=['views'])
context = {'title': 'Example', 'items': ['Haruhi', 'Mikuru', 'Yuki'] }
output = engine.render('example.pyhtml', context)
print(output)
## output
$ python main.py
<h2>Example</h2>
<table>
<tr class="odd">
<td>Haruhi</td>
</tr>
<tr class="even">
<td>Mikuru</td>
</tr>
<tr class="odd">
<td>Yuki</td>
</tr>
</table>
download: