Toggle navigation


Jinja2 supports extensions that can add extra filters, tests, globals or even extend the parser. The main motivation of extensions is to move often used code into a reusable class like adding support for internationalization.

Adding Extensions

Extensions are added to the Jinja2 environment at creation time. Once the environment is created additional extensions cannot be added. To add an extension pass a list of extension classes or import paths to the [UNKNOWN NODE title_reference] parameter of the Environment constructor. The following example creates a Jinja2 environment with the i18n extension loaded:

jinja_env = Environment(extensions=['jinja2.ext.i18n'])

i18n Extension

Import name: [UNKNOWN NODE title_reference]

The i18n extension can be used in combination with gettext or babel. If the i18n extension is enabled Jinja2 provides a [UNKNOWN NODE title_reference] statement that marks the wrapped string as translatable and calls [UNKNOWN NODE title_reference].

After enabling, dummy [UNKNOWN NODE title_reference] function that forwards calls to [UNKNOWN NODE title_reference] is added to the environment globals. An internationalized application then has to provide a [UNKNOWN NODE title_reference] function and optionally an [UNKNOWN NODE title_reference] function into the namespace, either globally or for each rendering.

Environment Methods

After enabling the extension, the environment provides the following additional methods:

jinja2.Environment.install_gettext_translations(translations, newstyle=False)

Installs a translation globally for that environment. The translations object provided must implement at least [UNKNOWN NODE title_reference] and [UNKNOWN NODE title_reference]. The [UNKNOWN NODE title_reference] and [UNKNOWN NODE title_reference] classes as well as Babels [UNKNOWN NODE title_reference] class are supported.

Changed in version 2.5: newstyle gettext added


Install dummy gettext functions. This is useful if you want to prepare the application for internationalization but don’t want to implement the full internationalization system yet.

Changed in version 2.5: newstyle gettext added

jinja2.Environment.install_gettext_callables(gettext, ngettext, newstyle=False)

Installs the given [UNKNOWN NODE title_reference] and [UNKNOWN NODE title_reference] callables into the environment as globals. They are supposed to behave exactly like the standard library’s gettext.ugettext() and gettext.ungettext() functions.

If [UNKNOWN NODE title_reference] is activated, the callables are wrapped to work like newstyle callables. See Whitespace Trimming for more information.

New in version 2.5.


Uninstall the translations again.


Extract localizable strings from the given template node or source.

For every string found this function yields a (lineno, function, message) tuple, where:

  • [UNKNOWN NODE title_reference] is the number of the line on which the string was found,
  • [UNKNOWN NODE title_reference] is the name of the [UNKNOWN NODE title_reference] function used (if the string was extracted from embedded Python code), and
  • [UNKNOWN NODE title_reference] is the string itself (a [UNKNOWN NODE title_reference] object, or a tuple of [UNKNOWN NODE title_reference] objects for functions with multiple string arguments).

If Babel is installed, the babel integration can be used to extract strings for babel.

For a web application that is available in multiple languages but gives all the users the same language (for example a multilingual forum software installed for a French community) may load the translations once and add the translation methods to the environment at environment generation time:

translations = get_gettext_translations()
env = Environment(extensions=['jinja2.ext.i18n'])

The [UNKNOWN NODE title_reference] function would return the translator for the current configuration. (For example by using [UNKNOWN NODE title_reference])

The usage of the [UNKNOWN NODE title_reference] extension for template designers is covered as part of the template documentation.

Whitespace Trimming

New in version 2.10.

Linebreaks and surrounding whitespace can be automatically trimmed by enabling the ext.i18n.trimmed policy.

Newstyle Gettext

New in version 2.5.

Starting with version 2.5 you can use newstyle gettext calls. These are inspired by trac’s internal gettext functions and are fully supported by the babel extraction tool. They might not work as expected by other extraction tools in case you are not using Babel’s.

What’s the big difference between standard and newstyle gettext calls? In general they are less to type and less error prone. Also if they are used in an autoescaping environment they better support automatic escaping. Here are some common differences between old and new calls:

standard gettext:

{{ gettext('Hello World!') }}
{{ gettext('Hello %(name)s!')|format(name='World') }}
{{ ngettext('%(num)d apple', '%(num)d apples', apples|count)|format(

newstyle gettext looks like this instead:

{{ gettext('Hello World!') }}
{{ gettext('Hello %(name)s!', name='World') }}
{{ ngettext('%(num)d apple', '%(num)d apples', apples|count) }}

The advantages of newstyle gettext are that you have less to type and that named placeholders become mandatory. The latter sounds like a disadvantage but solves a lot of troubles translators are often facing when they are unable to switch the positions of two placeholder. With newstyle gettext, all format strings look the same.

Furthermore with newstyle gettext, string formatting is also used if no placeholders are used which makes all strings behave exactly the same. Last but not least are newstyle gettext calls able to properly mark strings for autoescaping which solves lots of escaping related issues many templates are experiencing over time when using autoescaping.

Expression Statement

Import name: [UNKNOWN NODE title_reference]

The “do” aka expression-statement extension adds a simple [UNKNOWN NODE title_reference] tag to the template engine that works like a variable expression but ignores the return value.

Loop Controls

Import name: [UNKNOWN NODE title_reference]

This extension adds support for [UNKNOWN NODE title_reference] and [UNKNOWN NODE title_reference] in loops. After enabling, Jinja2 provides those two keywords which work exactly like in Python.

With Statement

Import name: [UNKNOWN NODE title_reference]

Changed in version 2.9.

This extension is now built-in and no longer does anything.

Autoescape Extension

Import name: [UNKNOWN NODE title_reference]

Changed in version 2.9.

This extension was removed and is now built-in. Enabling the extension no longer does anything.

Writing Extensions

By writing extensions you can add custom tags to Jinja2. This is a non-trivial task and usually not needed as the default tags and expressions cover all common use cases. The i18n extension is a good example of why extensions are useful. Another one would be fragment caching.

When writing extensions you have to keep in mind that you are working with the Jinja2 template compiler which does not validate the node tree you are passing to it. If the AST is malformed you will get all kinds of compiler or runtime errors that are horrible to debug. Always make sure you are using the nodes you create correctly. The API documentation below shows which nodes exist and how to use them.

Example Extension

The following example implements a [UNKNOWN NODE title_reference] tag for Jinja2 by using the Werkzeug caching contrib module:

from jinja2 import nodes
from jinja2.ext import Extension

class FragmentCacheExtension(Extension):
    # a set of names that trigger the extension.
    tags = set(['cache'])

    def __init__(self, environment):
        super(FragmentCacheExtension, self).__init__(environment)

        # add the defaults to the environment

    def parse(self, parser):
        # the first token is the token that started the tag.  In our case
        # we only listen to ``'cache'`` so this will be a name token with
        # `cache` as value.  We get the line number so that we can give
        # that line number to the nodes we create by hand.
        lineno = next(

        # now we parse a single expression that is used as cache key.
        args = [parser.parse_expression()]

        # if there is a comma, the user provided a timeout.  If not use
        # None as second parameter.

        # now we parse the body of the cache block up to `endcache` and
        # drop the needle (which would always be `endcache` in that case)
        body = parser.parse_statements(['name:endcache'], drop_needle=True)

        # now return a `CallBlock` node that calls our _cache_support
        # helper method on this extension.
        return nodes.CallBlock(self.call_method('_cache_support', args),
                               [], [], body).set_lineno(lineno)

    def _cache_support(self, name, timeout, caller):
        """Helper callback."""
        key = self.environment.fragment_cache_prefix + name

        # try to load the block from the cache
        # if there is no fragment in the cache, render it and store
        # it in the cache.
        rv = self.environment.fragment_cache.get(key)
        if rv is not None:
            return rv
        rv = caller()
        self.environment.fragment_cache.add(key, rv, timeout)
        return rv

And here is how you use it in an environment:

from jinja2 import Environment
from werkzeug.contrib.cache import SimpleCache

env = Environment(extensions=[FragmentCacheExtension])
env.fragment_cache = SimpleCache()

Inside the template it’s then possible to mark blocks as cacheable. The following example caches a sidebar for 300 seconds:

{% cache 'sidebar', 300 %}
<div class="sidebar">
{% endcache %}

Extension API

Extensions always have to extend the jinja2.ext.Extension class:

class jinja2.ext.Extension(environment)

Extensions can be used to add extra functionality to the Jinja template system at the parser level. Custom extensions are bound to an environment but may not store environment specific data on [UNKNOWN NODE title_reference]. The reason for this is that an extension can be bound to another environment (for overlays) by creating a copy and reassigning the [UNKNOWN NODE title_reference] attribute.

As extensions are created by the environment they cannot accept any arguments for configuration. One may want to work around that by using a factory function, but that is not possible as extensions are identified by their import name. The correct way to configure the extension is storing the configuration values on the environment. Because this way the environment ends up acting as central configuration storage the attributes may clash which is why extensions have to ensure that the names they choose for configuration are not too generic. prefix for example is a terrible name, fragment_cache_prefix on the other hand is a good name as includes the name of the extension (fragment cache).


The identifier of the extension. This is always the true import name of the extension class and must not be changed.


If the extension implements custom tags this is a set of tag names the extension is listening for.

attr(name, lineno=None)

Return an attribute node for the current extension. This is useful to pass constants on extensions to generated template code.

self.attr('_my_attribute', lineno=lineno)
call_method(name, args=None, kwargs=None, dyn_args=None, dyn_kwargs=None, lineno=None)

Call a method of the extension. This is a shortcut for attr() + jinja2.nodes.Call.


It’s passed a TokenStream that can be used to filter tokens returned. This method has to return an iterable of Tokens, but it doesn’t have to return a TokenStream.

In the [UNKNOWN NODE title_reference] folder of the Jinja2 source distribution there is a file called [UNKNOWN NODE title_reference] which implements a filter that utilizes this method.


If any of the tags matched this method is called with the parser as first argument. The token the parser stream is pointing at is the name token that matched. This method has to return one or a list of multiple nodes.

preprocess(source, name, filename=None)

This method is called before the actual lexing and can be used to preprocess the source. The [UNKNOWN NODE title_reference] is optional. The return value must be the preprocessed source.

Parser API

The parser passed to Extension.parse() provides ways to parse expressions of different types. The following methods may be used by extensions:

class jinja2.parser.Parser(environment, source, name=None, filename=None, state=None)

This is the central parsing class Jinja2 uses. It’s passed to extensions and can be used to parse expressions or statements.


The filename of the template the parser processes. This is not the load name of the template. For the load name see name. For templates that were not loaded form the file system this is [UNKNOWN NODE title_reference].


The load name of the template.


The current TokenStream

fail(msg, lineno=None, exc=<class 'jinja2.exceptions.TemplateSyntaxError'>)

Convenience method that raises [UNKNOWN NODE title_reference] with the message, passed line number or last line number as well as the current name and filename.


Return a new free identifier as InternalName.

parse_assign_target(with_tuple=True, name_only=False, extra_end_rules=None, with_namespace=False)

Parse an assignment target. As Jinja2 allows assignments to tuples, this function can parse all allowed assignment targets. Per default assignments to tuples are parsed, that can be disable however by setting [UNKNOWN NODE title_reference] to [UNKNOWN NODE title_reference]. If only assignments to names are wanted [UNKNOWN NODE title_reference] can be set to [UNKNOWN NODE title_reference]. The [UNKNOWN NODE title_reference] parameter is forwarded to the tuple parsing function. If [UNKNOWN NODE title_reference] is enabled, a namespace assignment may be parsed.


Parse an expression. Per default all expressions are parsed, if the optional [UNKNOWN NODE title_reference] parameter is set to [UNKNOWN NODE title_reference] conditional expressions are not parsed.

parse_statements(end_tokens, drop_needle=False)

Parse multiple statements into a list until one of the end tokens is reached. This is used to parse the body of statements as it also parses template data if appropriate. The parser checks first if the current token is a colon and skips it if there is one. Then it checks for the block end and parses until if one of the [UNKNOWN NODE title_reference] is reached. Per default the active token in the stream at the end of the call is the matched end token. If this is not wanted [UNKNOWN NODE title_reference] can be set to [UNKNOWN NODE title_reference] and the end token is removed.

parse_tuple(simplified=False, with_condexpr=True, extra_end_rules=None, explicit_parentheses=False)

Works like [UNKNOWN NODE title_reference] but if multiple expressions are delimited by a comma a Tuple node is created. This method could also return a regular expression instead of a tuple if no commas where found.

The default parsing mode is a full tuple. If [UNKNOWN NODE title_reference] is [UNKNOWN NODE title_reference] only names and literals are parsed. The [UNKNOWN NODE title_reference] parameter is forwarded to parse_expression().

Because tuples do not require delimiters and may end in a bogus comma an extra hint is needed that marks the end of a tuple. For example for loops support tuples between [UNKNOWN NODE title_reference] and [UNKNOWN NODE title_reference]. In that case the [UNKNOWN NODE title_reference] is set to ['name:in'].

[UNKNOWN NODE title_reference] is true if the parsing was triggered by an expression in parentheses. This is used to figure out if an empty tuple is a valid expression or not.

class jinja2.lexer.TokenStream(generator, name, filename)

A token stream is an iterable that yields Tokens. The parser however does not iterate over it but calls next() to go one token ahead. The current active token is stored as current.


The current Token.


Are we at the end of the stream?


Expect a given token type and return it. This accepts the same argument as jinja2.lexer.Token.test().


Look at the next token.


Perform the token test and return the token if it matched. Otherwise the return value is [UNKNOWN NODE title_reference].


Push a token back to the stream.


Got n tokens ahead.


Like next_if() but only returns [UNKNOWN NODE title_reference] or [UNKNOWN NODE title_reference].

class jinja2.lexer.Token

Token class.


The line number of the token


The type of the token. This string is interned so you may compare it with arbitrary strings using the [UNKNOWN NODE title_reference] operator.


The value of the token.


Test a token against a token expression. This can either be a token type or 'token_type:token_value'. This can only test against string values and types.


Test against multiple token expressions.

There is also a utility function in the lexer module that can count newline characters in strings:


Count the number of newline characters in the string. This is useful for extensions that filter a stream.


The AST (Abstract Syntax Tree) is used to represent a template after parsing. It’s build of nodes that the compiler then converts into executable Python code objects. Extensions that provide custom statements can return nodes to execute custom Python code.

The list below describes all nodes that are currently available. The AST may change between Jinja2 versions but will stay backwards compatible.

For more information have a look at the repr of jinja2.Environment.parse().

exception jinja2.nodes.Impossible

Raised if the node could not perform a requested action.