Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

354 lines (235 sloc) 10.222 kB



:term:`pyramid_jinja2` is a set of bindings that make templates written for the :term:`Jinja2` templating system work under the :term:`Pyramid` web framework.


Install using setuptools, e.g. (within a virtualenv):

$ $myvenv/bin/easy_install pyramid_jinja2


There are two ways to make sure that pyramid_jinja2 is active. Both are completely equivalent:

  1. Use the includeme function via config.include:

  2. If you're using pyramid_zcml instead of imperative configuration, ensure that some ZCML file with an analogue of the following contents is executed by your Pyramid application:

    <include package="pyramid_jinja2"/>

Once activated either of these says, the following happens:

  1. Files with the .jinja2 extension are considered to be :term:`Jinja2` templates.
  2. The :func:`pyramid_jinja2.add_jinja2_search_path` directive is added to the :term:`configurator` instance.
  3. The :func:`pyramid_jinja2.add_jinja2_extension` directive is added to the :term:`configurator` instance.
  4. The :func:`pyramid_jinja2.get_jinja2_environment` directive is added to the :term:`configurator` instance.
  5. jinja2.Environment is constructed and registered globally.

To setup the jinja2 search path either one of the following steps must be taken:

  1. Add jinja2.directories to your .ini settings file using the pyramid asset spec:

    jinja2.directories = yourapp:templates
  2. Or Alternatively by using the add_jinja2_search_path directive attached to your application's :term:`configurator` instance also using the pyramid asset spec:



If you do not explicitly configure your jinja2 search path it will default to the root of your application. If the specified template is not found in the root of your application and you did not specify a package on the template path it will then try to load the template path relative to the module's caller package. For example:

Without the search path configured:


With the search path configured:


If you view module is in app.module.view and your template is under app/module/templates/mytemplate.jinja2 you can access that asset in a few different ways.

Using the full path:


Using the package:


Using the relative path to current package:


You need to be careful when using relative paths though, if there is an app/templates/mytemplate.jinja2 this will be used instead as jinja2 lookup will first try the path relative to the root of the app and then it will try the path relative to the current package.


Once :term:`pyramid_jinja2` been activated .jinja2 templates can be loaded either by looking up names that would be found on the :term:`Jinja2` search path or by looking up asset specifications.

Template Lookups

The default lookup mechanism for templates uses the :term:`Jinja2` search path. (specified with jinja2.directories or by using the add_jinja2_search_path directive on the :term:`configurator` instance.)

Rendering :term:`Jinja2` templates with a view like this is typically done as follows (where the templates directory is expected to live in the search path):

Rendering templates outside of a view (and without a request) can be done using the renderer api:

:term:`Template Inheritance`

:term:`Template inheritance` can use asset specs in the same manner as regular template lookups. An example:

For further information on :term:`template inheritance` in Jinja2 templates please see

Asset Specification Lookups

Looking up templates via asset specification is a feature specific to :term:`Pyramid`. For further info please see Understanding Asset Specifications. Overriding templates in this style uses the standard pyramid asset overriding technique.


Jinja2 derives additional settings to configure its template renderer. Many of these settings are optional and only need to be set if they should be different from the default. The below values can be present in the .ini file used to configure the Pyramid application (in the app section representing your Pyramid app) or they can be passed directly within the settings argument passed to a Pyramid Configurator.


true or false representing whether Jinja2 templates should be reloaded when they change on disk. Useful for development to be true.


A list of directory names or a newline-delimited string with each line representing a directory name. These locations are where Jinja2 will search for templates. Each can optionally be an absolute resource specification (e.g. package:subdirectory/).


The input encoding of templates. Defaults to utf-8.


true or false representing whether Jinja2 will autoescape rendered blocks. Defaults to true.


A list of extension objects or a newline-delimited set of dotted import locations where each line represents an extension. jinja2.ext.i18n is automatically activated.


Pyramid domain for translations. See


A dictionary mapping filter name to filter object, or a newline-delimted string with each line in the format name = representing Jinja2 filters.


true or false to enable filesystem bytecode caching. Defaults to true. See


Absolute path to directory to store bytecode caching files. Defaults to temporary directory. See

Jinja2 Filters

pyramid_jinja2 provides two filters.

To use these filters, configure the settings of jinja2.filters:

And use the filters in template.

<a href="{{context|model_url('edit')}}">Edit</a>

<a href="{{'top'|route_url}}">Top</a>

<link rel="stylesheet" href="{{'yourapp:static/css/style.css'|static_url}}" />

Creating a Jinja2 Pyramid Project

After you've got pyramid_jinja2 installed, you can invoke one of the following commands to create a Jinja2-based Pyramid project.

On Pyramid 1.0, 1.1, or 1.2:

$ $myvenv/bin/paster create -t pyramid_jinja2_starter myproject

On Pyramid 1.3:

$ $myenv/bin/pcreate -s pyramid_jinja2_starter myproject

After it's created, you can visit the myproject directory and run develop. At that point you can start the application like any other Pyramid application.

This is a good way to see a working Pyramid application that uses Jinja2, even if you wind up not using the result.

Paster Template I18N

The paster template automatically sets up pot/po/mo locale files for use with the generated project.

The usual pattern for working with i18n in pyramid_jinja2 is as follows:

More Information

Reporting Bugs / Development Versions

Visit to download development or tagged versions.

Visit to report bugs.

Indices and tables

Jump to Line
Something went wrong with that request. Please try again.