[Webpy]在webpy中使用jinja2模板

来源:互联网 发布:centos无法挂载ntfs 编辑:程序博客网 时间:2024/06/06 03:11

webpy的模板感觉写小的网页还可以,但是如果要是写比较多的html标签就会显得非常乱,于是决定使用jinja2,这个模板类似django的模板,而且跟其他pythonweb框架的兼容性也比较好。

在项目目录下新建一个settings文件

#-*- coding: utf-8 -*-__author__  = 'orangleliu''''settings of the project'''import osimport webfrom web.contrib.template import render_jinja#------------------debug----------------------debug = 'SERVER_SOFTWARE' not in os.environweb.config.debug = debug#------------------jinja2----------------------app_root = os.path.dirname(__file__)templates_path = os.path.join(app_root, 'templates').replace('\\', '/')render = render_jinja(    templates_path,    encoding='utf-8')

要在项目目录下新建一个templates文件夹来存放模板文件。


使用:

#-*- coding: utf-8 -*-__author__ = 'orangleliu''''filename: index.pycreate: @20140513index page handler of this app'''import webfrom settings import renderclass index:    def GET(self):        web.header("Content-Type", "text/html")        return render.index({})


项目文件的结构如下:



这样就可以使用jinja2模板了。




0 0