基于hi-nginx的web开发(python篇)——使用jinja2模板引擎
模板引擎的使用在web开发中是不可避免和必要的。hi.py框架使用jinja2作为模板引擎。 为了使用hi.py提供的jinja2引擎,首先需要引入它: from hi import hi,template 然后就是使用它: 1 @app.route(r'^/template/(?P<name>\w+)/(?P<age>\d+)/?$',['GET']) 2 def tpl(req,res,param): 3 param['title']='jinja2 测试' 4 tpl_engine = template(os.path.join(os.getcwd(),'python/templates')) 5 res.content(tpl_engine.file_render('b.html',param)) 6 res.status(200) 创建template实例需要一个参数,它指定引擎搜索模板文件的目录,在上面的代码中就是hi-nginx安装目录下的python/templates文件夹。 然后准备数据,数据使用dict来收集,使用file_render方法...