首页 > Web开发 > 详细

[Webpy]在webpy中使用jinja2模板

时间:2014-05-15 03:23:23      阅读:383      评论:0      收藏:0      [点我收藏+]

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

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

#-*- coding: utf-8 -*-
__author__  = ‘orangleliu‘

‘‘‘
settings of the project
‘‘‘

import os
import web
from web.contrib.template import render_jinja

#------------------debug----------------------
debug = ‘SERVER_SOFTWARE‘ not in os.environ
web.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.py
create: @20140513
index page handler of this app
‘‘‘

import web
from settings import render

class index:
    def GET(self):
        web.header("Content-Type", "text/html")
        return render.index({})


项目文件的结构如下:

bubuko.com,布布扣


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




[Webpy]在webpy中使用jinja2模板,布布扣,bubuko.com

[Webpy]在webpy中使用jinja2模板

原文:http://blog.csdn.net/orangleliu/article/details/25776901

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!