Sphinx Jinja

2.0.2 · active · verified Fri Apr 10

Sphinx Jinja is a Python library that enables the embedding of Jinja2 templates directly within Sphinx documentation. It allows dynamic content generation, variable injection, and custom logic using Jinja2 syntax in reStructuredText or MyST files. The current version is 2.0.2, and releases are generally made as needed, with significant changes between major versions.

Warnings

Install

Imports

Quickstart

To get started, add 'sphinx_jinja' to your `extensions` list in `conf.py`. Then, configure Jinja contexts, filters, globals, and environment options using `jinja_contexts`, `jinja_filters`, `jinja_globals`, `jinja_tests`, and `jinja_env_kwargs` variables. Finally, use the `.. jinja::` directive in your reStructuredText or MyST files to render templates. The provided example shows a `conf.py` setup and a conceptual `.rst` usage.

# conf.py
project = 'My Jinja Project'
copyright = '2024, Your Name'
html_theme = 'alabaster'
extensions = [
    'sphinx_jinja'
]

jinja_contexts = {
    'global_vars': {
        'product_name': 'Awesome App',
        'version': '1.0.0'
    }
}

jinja_filters = {
    'upper': lambda s: s.upper()
}

jinja_env_kwargs = {
    'trim_blocks': True,
    'lstrip_blocks': True
}

# docs/index.rst
# .. jinja:: global_vars
#
#    Welcome to {{ product_name }} ({{ version }}).
#    This text will be uppercased: {{ "hello world" | upper }}

view raw JSON →