Python 为cherrypy创建一个猎豹工具
我刚开始玩cherrypy,想用猎豹作为模板引擎 因此,我想创建一个工具,这样我就可以使用注释功能指向我的模板 差不多Python 为cherrypy创建一个猎豹工具,python,cherrypy,cheetah,Python,Cherrypy,Cheetah,我刚开始玩cherrypy,想用猎豹作为模板引擎 因此,我想创建一个工具,这样我就可以使用注释功能指向我的模板 差不多 import cherrypy class Root(object): @cherrypy.expose() @cherrypy.tools.cheetah(template="index") def index(self): title = "Demo" content = "Stuff" retur
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)
....
但是我不想先编译模板。我想从公开的站点返回我的内容。我的猎豹工具现在应该拦截这些内容并创建模板
我知道如何创建模板:
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)
但我已经用一个简短的演示试过了<代码>设置属性不工作。我这样试过:
template = Template(file = filename)
for key, value in data:
setattr(template, 'title', 'Test')
有人能给我指一下正确的方向吗?可能是重复的