我刚开始玩樱桃,想用猎豹作为模板引擎。
因此我想创建一个工具,这样我就可以使用注释功能指向我的模板
类似
import cherrypy
class Root(object):
@cherrypy.expose()
@cherrypy.tools.cheetah(template="index")
def index(self):
title = "Demo"
content = "Stuff"
return {'title': title, 'content': content}
我已经在cherrypywiki上找到了可以使用编译模板的东西:
但我不想先编译模板。我想从我公开的网站上返回我的内容。我的猎豹工具现在应该截取该内容并创建模板
我知道如何创建模板:
from Cheetah.Template import Template
....
cherrypy.expose()
def demo(self):
filename = os.path.join(APPDIR, "index.tmpl")
template = Template(file = filename)
template.content = "bla"
template.title = "Test"
return str(template)
....
现在基本上在我的页面处理程序中,我只返回我的内容字典,我的工具创建模板并动态填充属性。由于我也是Python的新手,我不知道如何动态地执行此操作。
我希望我可以遍历我的词典并在我的工具中做类似的事情:
template = Template(file = filename)
for key, value in data:
setattr(template, key, value)
但我已经尝试了一个简短的演示。 setattr
无效。我试过这样的话:
template = Template(file = filename)
for key, value in data:
setattr(template, 'title', 'Test')
有人能指出我正确的方向吗?