ローカルホストで Mako テンプレート エンジンを使い始めました。ただし、テンプレートを選択するたびに、テンプレートの完全な場所を入力する必要があります (例: c:/Users/username/Desktop/xampp/htdocs/blog/scripts/templates/index.html' )。たとえば、毎回「scripts/templates/index.html」などと入力するなど、変更したいと考えています。
それを行う方法はありますか?
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)