Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 5ab2b051c4
Fetching contributors…

Cannot retrieve contributors at this time

76 lines (54 sloc) 2.05 kb
layout title
Use Jinja2 template engine in webpy

Use Jinja2 template engine in webpy


How to use Jinja2 ( template engine in webpy?

Solution 1

You need to install both Jinja2 and webpy(0.3) first, and then try out the following code snippet:

import web
from web.contrib.template import render_jinja

urls = (
        '/(.*)', 'hello'

app = web.application(urls, globals())

render = render_jinja(
        'templates',   # Set template directory.
        encoding = 'utf-8',                         # Encoding.

# Add/override some global functions.
#       var=newvar,
#       var2=newvar2,

class hello:
    def GET(self, name):
        return render.hello(name=name)

if __name__ == "__main__":

Template file: templates/hello.html

Hello, {{ name }}.

Solution 2

With this solution, you have to specify template name, it's useful if template files are organized in different directories.

import os
import web
from jinja2 import Environment,FileSystemLoader

urls = ("/.*", "hello")
app = web.application(urls, globals())

def render_template(template_name, **context):
    extensions = context.pop('extensions', [])
    globals = context.pop('globals', {})

    jinja_env = Environment(
            loader=FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates')),

    return jinja_env.get_template(template_name).render(context)

class hello:
    def GET(self):
        # You can use a relative path as template name, for example, 'ldap/hello.html'.
        return render_template('hello.html', name='world',)

if __name__ == "__main__":
Jump to Line
Something went wrong with that request. Please try again.