Jinja2

This module provides Jinja2 template support for webapp2.

To use it, you must add the jinja2 package to your application directory (for App Engine) or install it in your virtual environment (for other servers).

You can download jinja2 from PyPi:

http://pypi.python.org/pypi/Jinja2

Learn more about Jinja2:

http://jinja.pocoo.org/
webapp2_extras.jinja2.default_config

Default configuration values for this module. Keys are:

template_path
Directory for templates. Default is templates.
compiled_path
Target for compiled templates. If set, uses the loader for compiled templates in production. If it ends with a ‘.zip’ it will be treated as a zip file. Default is None.
force_compiled
Forces the use of compiled templates even in the development server.
environment_args
Keyword arguments used to instantiate the Jinja2 environment. By default autoescaping is enabled and two extensions are set: jinja2.ext.autoescape and jinja2.ext.with_. For production it may be a good idea to set ‘auto_reload’ to False – we don’t need to check if templates changed after deployed.
globals
Extra global variables for the Jinja2 environment.
filters
Extra filters for the Jinja2 environment.
class webapp2_extras.jinja2.Jinja2(app, config=None)[source]

Wrapper for configurable and cached Jinja2 environment.

To used it, set it as a cached property in a base RequestHandler:

import webapp2

from webapp2_extras import jinja2

class BaseHandler(webapp2.RequestHandler):

    @webapp2.cached_property
    def jinja2(self):
        # Returns a Jinja2 renderer cached in the app registry.
        return jinja2.get_jinja2(app=self.app)

    def render_response(self, _template, **context):
        # Renders a template and writes the result to the response.
        rv = self.jinja2.render_template(_template, **context)
        self.response.write(rv)

Then extended handlers can render templates directly:

class MyHandler(BaseHandler):
    def get(self):
        context = {'message': 'Hello, world!'}
        self.render_response('my_template.html', **context)
__init__(app, config=None)[source]

Initializes the Jinja2 object.

Parameters:
render_template(_filename, **context)[source]

Renders a template and returns a response object.

Parameters:
  • _filename – The template filename, related to the templates directory.
  • context – Keyword arguments used as variables in the rendered template. These will override values set in the request context.
Returns:

A rendered template.

get_template_attribute(filename, attribute)[source]

Loads a macro (or variable) a template exports. This can be used to invoke a macro from within Python code. If you for example have a template named _foo.html with the following contents:

{% macro hello(name) %}Hello {{ name }}!{% endmacro %}

You can access this from Python code like this:

hello = get_template_attribute('_foo.html', 'hello')
return hello('World')

This function comes from Flask.

Parameters:
  • filename – The template filename.
  • attribute – The name of the variable of macro to acccess.
webapp2_extras.jinja2.get_jinja2(factory=<class 'webapp2_extras.jinja2.Jinja2'>, key='webapp2_extras.jinja2.Jinja2', app=None)[source]

Returns an instance of Jinja2 from the app registry.

It’ll try to get it from the current app registry, and if it is not registered it’ll be instantiated and registered. A second call to this function will return the same instance.

Parameters:
  • factory – The callable used to build and register the instance if it is not yet registered. The default is the class Jinja2 itself.
  • key – The key used to store the instance in the registry. A default is used if it is not set.
  • app – A webapp2.WSGIApplication instance used to store the instance. The active app is used if it is not set.
webapp2_extras.jinja2.set_jinja2(jinja2, key='webapp2_extras.jinja2.Jinja2', app=None)[source]

Sets an instance of Jinja2 in the app registry.

Parameters:
  • store – An instance of Jinja2.
  • key – The key used to retrieve the instance from the registry. A default is used if it is not set.
  • request – A webapp2.WSGIApplication instance used to retrieve the instance. The active app is used if it is not set.

Previous topic

i18n

Next topic

JSON

This Page