一般放在 <butterfly_project>/templates 下
<p>Welcome, {{user_name}}!</p>
<p>Products:</p>
<ul>
{% for product in product_list %}
<li>{{ product.name }}:
{{ product.price|format_price }}</li>
{% endfor %}
</ul>
from templite import Templite
from collections import namedtuple
def demo(req):
with open("./templates/host_unit.tpl", "r") as f:
template_text = f.read()
Product = namedtuple("Product",["name", "price"])
product_list = [Product("Apple", 1), Product("Fig", 1.5), Product("Pomegranate", 3.25)]
def format_price(price):
return "$%.2f" % price
# 解析模板
t = Templite(template_text, {"user_name":"Charlie", "product_list":product_list}, {"format_price":format_price})
# 渲染模板
print t.render()
if __name__ == "__main__":
demo()