我建议使用Python提供的许多模板语言之一,例如一个built into Django(你没有使用Django的其余部分来使用它的模板引擎) - 谷歌查询应该给你很多其他的替代模板实现。
我发现学习模板库有很多方面的帮助 - 无论何时您需要生成电子邮件,HTML页面,文本文件或类似文件,您只需编写一个模板,将其加载到模板库中,然后让模板代码创建成品。
下面是一些简单的代码,让你开始:
#!/usr/bin/env python
from django.template import Template, Context
from django.conf import settings
settings.configure() # We have to do this to use django templates standalone - see
# http://stackoverflow.com/questions/98135/how-do-i-use-django-templates-without-the-rest-of-django
# Our template. Could just as easily be stored in a separate file
template = """
Template {{ title }}Body with {{ mystring }}.
"""
t = Template(template)
c = Context({"title": "title from code",
"mystring":"string from code"})
print t.render(c)
,如果你有在磁盘上的模板这是更简单 - 检查出render_to_string功能,可以从搜索路径预设列表从磁盘加载模板,填写数据从一个胜利和呈现到一个字符串 - 所有在一个函数调用。