我开始在我的localhost上使用Mako模板引擎。但是,每次我想选择模板时,我都需要输入模板的完整位置(例如 c:/Users/username/Desktop/xampp/htdocs/blog/scripts/templates/index.html' )。我想更改它,例如每次只输入'scripts / templates / index.html'或类似内容。
有任何建议怎么做?
答案 0 :(得分:0)
使用mako的TemplateLookup课程。对于您的示例,请使用文档中的示例:
from mako.template import Template
from mako.lookup import TemplateLookup
template_path = "c:/Users/username/Desktop/xampp/htdocs/blog"
mylookup = TemplateLookup(directories=[path])
def serve_template(templatename, **kwargs):
templatename = "scripts/templates/index.html"
mytemplate = mylookup.get_template(templatename)
print mytemplate.render(**kwargs)