Sphinx Jinja
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
- breaking The configuration parameters in `conf.py` for setting up Jinja2 environments, contexts, filters, globals, and tests have changed significantly in version 2.0.0.
- breaking Version 2.0.0 dropped support for Python 3.6. It now requires Python 3.7 or newer.
- breaking Version 2.0.0 tightened its dependency on Sphinx, requiring Sphinx 4.0.0 or newer.
- breaking Version 2.0.0 updated its dependency on Jinja2, requiring Jinja2 3.0.0 or newer.
Install
-
pip install sphinx-jinja
Imports
- Extension Configuration
extensions = ['sphinx_jinja'] jinja_contexts = {...}
Quickstart
# 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 }}