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) 1.132 kb
d9a831e edit
Bibby authored
1 ---
2 layout: default
3 title: Use Jinja2 template engine in webpy
4 ---
5
6 # Use Jinja2 template engine in webpy
7
8 ### Problem
9 How to use Jinja2 (http://jinja.pocoo.org/2/) template engine in webpy?
10
11 ### Solution
12
13 You need to install both Jinja2 and webpy(0.3) first, and then try out the following code snippet:
14
15 import web
16 from web.contrib.template import render_jinja
17
18 urls = (
19 '/(.*)', 'hello'
20 )
21
22 app = web.application(urls, globals())
23
8d3945e edit
Bibby authored
24 render = render_jinja(
25 'templates', # Set template directory.
26 #extensions = ['jinja2.ext.i18n'], # Jinja2 extensions.
27 #encoding = 'utf-8', # Encoding.
28 #globals = {},
29 )
30
31 # Used for Jinja2 i18n extension.
32 #lang = gettext.translation('domain', 'localedir', languages=['zh_CN'])
33 #render._lookup.install_gettext_translations(default_lang)
34
d9a831e edit
Bibby authored
35 class hello:
36 def GET(self, name):
37 return render.hello(name=name)
38
39 if __name__ == "__main__":
40 app.run()
41
42 ### Template file: templates/hello.html
43 Hello, {{ name }}.
44
Something went wrong with that request. Please try again.