sphinx-jinja2

A sphinx extension to add the jinja directive, for rendering Jinja templates (powered by minijinja).

Note

This is an adaption of sphinx-jinja, which appears to be unmaintained.

See also

The ub-template directive in sphinx-ub-directives renders minijinja templates with the same :file:, :ctx:, :debug: and :raw: options, and with its configuration in ubproject.toml, the same project also renders in ubCode. It is stricter by design: contexts are data only (no env, Python objects or functions), there are no custom filters, tests or environment options, and template output may not contain headings. The two extensions can be used side by side. sphinx-jinja2 remains maintained with bug fixes and compatibility updates, while new template features are developed in sphinx-ub-directives, which is developed alongside sphinx-needs and also works in ubCode; to switch, see Migrating from sphinx-jinja2.

Usage

PyPI

Simply install and add sphinx_jinja2 to your conf.py extensions list.

pip install sphinx-jinja2
extensions = [
    ...
    'sphinx_jinja2',
    ...
]

Guide

The most basic usage is to render an inline template using the jinja directive, where the ctx option is a JSON dict that will be passed to the template as the context:

Example 1

.. jinja::
    :ctx: {"name": "World"}

    Hallo {{ name }}!

Hallo World!

The Sphinx environment is also available by default as env:

Example 2

.. jinja::

    This is {{ env.config.project }} version {{ env.config.version }}.

This is sphinx-jinja2 version 0.1.0.

The env key is reserved for the Sphinx environment. A context may still define env to override it (a jinja2.config warning is emitted when it does).

To set globally available variables, use the jinja2_contexts option in your conf.py, and refer to them by name as the first argument to the jinja directive:

Example 3

conf.py:
jinja2_contexts = {"ctx1": {"name": "World"}}
.. jinja:: ctx1

    Hallo {{ name }}!

Hallo World!

Templates from files

To load a template from a file, use the file option. Similar to the include directive, the path is relative to the source file that contains the directive, or relative to the source directory if the path starts with /. The file should always be encoded in UTF-8. If these files change then Sphinx will re-build pages that use them!

Example 4

templates/example1.jinja:
Hallo {{ name }}!
.. jinja::
    :file: templates/example1.jinja
    :ctx: {"name": "World"}

Hallo World!

Templates can include or extend other templates. Referenced templates are always relative to the source directory, and Sphinx will also correctly re-build pages that use them. Templates can never be loaded from outside the source directory (absolute paths and .. parent traversal are rejected).

Example 5

templates/example1.jinja:
Hallo {{ name }}!
.. jinja::
    :ctx: {"name": "World", "more": "content"}

    {% include "templates/example1.jinja" %}

    More {{ more }}!

Hallo World!

More content!

Headings in templates

Rendered templates are parsed within the context of the current document, and so heading levels are relative to the current document.

Example 6

.. jinja::

    Sub-heading
    ...........

    Content

Sub-heading

Content

If you need a generic template containing a heading, then perhaps use a context variable to specify the heading character:

Example 7

.. jinja::
    :ctx: {"heading_char": "."}

    Sub-heading
    {{ heading_char * 11 }}

    Content

Sub-heading

Content

Custom filters and tests

Custom filters and tests can be added to the environment, using the jinja2_filters and jinja2_tests configuration options. These map names to Python functions, or import strings of the form "module.path:func_name". Import strings are preferred, since Sphinx cannot cache function objects, meaning that full re-builds are always triggered.

Example 8

conf.py:
jinja2_tests = {"is_big": "my_module:is_big"}
.. jinja::
    :ctx: {"number": 200}

    {% if number is is_big %}{{ number }} is big!{% endif %}

200 is big!

Raw output

By default, the rendered template is parsed as source input, to instead output it as raw content of a given format, use the raw option:

Example 9

.. jinja::
    :ctx: {"name": "World"}
    :raw: html

    Hello <em>{{ name }}</em>!
Hello World!

MyST Markdown documents

The jinja directive can also be used within MyST Markdown documents, in which case the rendered template is parsed as MyST Markdown:

```{jinja}
:ctx: {"name": "World"}

Hello *{{ name }}*!
```

Debugging

To see the rendered templates in the built documentation, use the debug option for a single directive, or the jinja2_debug option in your conf.py to enable it globally:

Example 10

.. jinja::
    :ctx: {"name": "World"}
    :debug:

    Hallo **{{ name }}**!
Hallo **World**!

Hallo World!

Warning messages

Warning messages are displayed in the Sphinx build output for problematic inputs. These all have the type jinja2, with a subtype identifying the category of problem:

  • jinja2.config – configuration problems (an unknown or malformed context, ctx option, jinja2_env_kwargs, filter/test, …)

  • jinja2.template – problems reading a template file

  • jinja2.render – problems rendering a template

These types can be used to suppress them in the Sphinx configuration, either all together or by subtype:

suppress_warnings = ["jinja2"]  # suppress all
suppress_warnings = ["jinja2.render"]  # suppress only render errors

Rendering errors (such as an undefined variable) are reported at the line where they occur:

  • In reStructuredText documents, an inline template’s rendering error is attributed to the actual failing line in the document (or in the file it comes from, when the directive is in a file pulled in by .. include::).

  • For a template file (file option), and for an error inside another template that an inline template loads (with {% extends %} or {% import %}), the error is reported at the jinja directive’s line, and the position within that template file is appended to the message (e.g. (template.jinja:2)).

  • Problems with parsing the rendered content (as opposed to rendering it) are difficult / impossible to map back to the template source, and so refer to the first line number either of the jinja directive, or the template file (when using the file option).

  • In MyST Markdown documents, rendering errors are reported at the jinja directive’s line; problems with parsing the rendered content refer to a line number within the rendered content, offset from the jinja directive (and with myst-parser >=5, they are always attributed to the document containing the directive, rather than the template file).

Migration from Jinja2

Since v0.1.0, templates are rendered using minijinja, a modern re-implementation of the Jinja template engine, rather than Jinja2. Most templates will render identically, but note the following differences:

  • jinja2_env_kwargs are now passed to minijinja.Environment. Common keyword arguments (trim_blocks, lstrip_blocks, keep_trailing_newline, custom delimiters, …) are unchanged; Jinja2-only arguments are ignored, with a warning.

  • Undefined variables raise errors (as previously, with StrictUndefined), but this can now be relaxed with jinja2_env_kwargs = {"undefined_behavior": "lenient"}.

  • How some values render depends on the installed minijinja version (any minijinja>=2.15, including 3.x, is supported). None and booleans render as with Jinja2 (None, True) from minijinja 2.22, and as none, true before it. Before minijinja 3.0, strings in lists and dicts created in a template are double-quoted ({{ ['a', 1] }} gives ["a", 1], where Jinja2 gives ['a', 1]), and tuples render as lists. Lists and dicts passed in from Python keep their Python form on every version. From minijinja 3.0, the round filter rounds halves to even ({{ 2.5 | round }} is 2.0); before 3.0, it rounds halves away from zero (3.0). Use {% if var %}{{ var }}{% endif %} guards or explicit formatting if this matters.

  • A small number of Jinja2-only filters (e.g. urlize, xmlattr, center, wordcount) are not built in to minijinja; they can be re-added via jinja2_filters if required. minijinja has a built-in wordwrap only from 2.24, and it takes keyword arguments only (wordwrap(width=79); Jinja2’s wordwrap(79) fails); a wordwrap added via jinja2_filters replaces it.

  • Custom filters and tests receive values as plain arguments; Jinja2’s @pass_context, @pass_environment and @pass_eval_context decorators are not supported by minijinja (such filters fail when the template is rendered, with a warning, or, if their extra parameters have defaults, run with the wrong arguments).

  • Underscore-prefixed attributes (e.g. __class__, _private) are not accessible from templates; they are undefined, so the template fails to render, with a warning (with the default strict undefined behaviour; with lenient they render empty). Objects in a context (including env) still expose their public attributes and methods, so only put objects in a context that you trust a template with.

  • Python methods on objects (e.g. {{ "a,b".split(",") }}) continue to work, via minijinja’s Python compatibility mode.

Configuration

The following global configuration variables are available:

Configuration Options

jinja2_contexts:

A mapping of context names to context variables (default: {})

jinja2_env_kwargs:

Keyword arguments passed to minijinja.Environment (see https://github.com/mitsuhiko/minijinja/tree/main/minijinja-py) (default: {})

jinja2_filters:

A mapping of filter names to filter functions, or import strings like 'module.path:func_name' (preferred, since it is cacheable) (default: {})

jinja2_tests:

A mapping of test names to test functions, or import strings like 'module.path:func_name' (preferred, since it is cacheable) (default: {})

jinja2_debug:

Output the rendered template (default: False)