Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 45 lines (31 sloc) 0.93 kb
f50f987 吼吼 / Chao Shen edit
ohsc authored
1 ---
2 layout: default
3 title: Use Jinja2 template engine in webpy
4 ---
5
6 # Use Jinja2 template engine in webpy
7
3c0875a edit
David Xie authored
8 ### 问题
f50f987 吼吼 / Chao Shen edit
ohsc authored
9
3c0875a edit
David Xie authored
10 如何在web.py中使用Jinja2 (http://jinja.pocoo.org/2/) 模板引擎?
f50f987 吼吼 / Chao Shen edit
ohsc authored
11
3c0875a edit
David Xie authored
12 ### 方案
13
14 首先需要安装Jinja2和webpy(0.3), 然后使用下面的代码做测试:
f50f987 吼吼 / Chao Shen edit
ohsc authored
15
16 import web
17 from web.contrib.template import render_jinja
18
19 urls = (
20 '/(.*)', 'hello'
21 )
22
23 app = web.application(urls, globals())
24
25 render = render_jinja(
3c0875a edit
David Xie authored
26 'templates', # 设置模板路径.
27 encoding = 'utf-8', # 编码.
f50f987 吼吼 / Chao Shen edit
ohsc authored
28 )
29
3c0875a edit
David Xie authored
30 #添加或者修改一些全局方法.
f50f987 吼吼 / Chao Shen edit
ohsc authored
31 #render._lookup.globals.update(
32 # var=newvar,
33 # var2=newvar2,
34 #)
35
36 class hello:
37 def GET(self, name):
38 return render.hello(name=name)
39
40 if __name__ == "__main__":
41 app.run()
42
3c0875a edit
David Xie authored
43 ### 模板文件: templates/hello.html
f50f987 吼吼 / Chao Shen edit
ohsc authored
44
45 Hello, {{ name }}.
Something went wrong with that request. Please try again.