genshi渲染lribeiro.cherrypy.templating
lribeiro.cherrypy.templating.genshi的Python项目详细描述
用于lribeiro.cherrypy.templating
的genshi渲染器它有三个可选配置项:
- templating.genshi.template_目录:可以是templates目录的路径、路径列表或genshi.template.TemplateLoader接受的任何内容。如果没有给定,则使用{root module directory}/templates作为模板目录。
- templating.genshi.loader_选项:dict包含传递给templateloader构造函数的其他参数
- templating.genshi.render_选项:dict包含呈现模板时使用的参数,例如,{'method':'html','doctype':'html transitional'}
您还可以通过@templatedecorator args和kwargs向呈现传递参数。
classController:@cherrypy.expose@template('template_name.html',method='xhtml',doctype='xhtml11')defhandler(self):pass
设置模板名称时,请记住genshi将前导斜杠解释为绝对路径!因此,下面可能会生成一个模板notfound:
classWrongController:@cherrypy.expose# pay attention to the leading slash# Genshi will look for a 'wrong_template_name.html' file on the root of the filesystem@template('/wrong_template_name.html')defwrong_handler(self):pass
在python3.4下开发,并对python2.7、python3.4和pypy进行了测试。
用法:
importcherrypyfromlribeiro.cherrypy.templatingimporttemplatefromlribeiro.cherrypy.templating.genshiimportrendererclassRoot(object):@cherrypy.expose@template('index.html')defindex(self):return{'context':'variables'}@cherrypy.expose@template('/var/templates/page.html')# absolute pathdefpage(self):return{'context':'variables'}config={'/':{'templating.renderer':renderer,'templating.genshi.template_directories':['templates',# relative to the module where the root class was declared'/var/templates'# absolute path],'templating.genshi.loader_options':{'default_encoding':'utf-8'},# this is optional'templating.genshi.render_options':{'method':'html','doctype':'html5'}# this is also optional}}if__name__=='__main__':cherrypy.quickstart(Root(),'/',config)