Skip to main content

includes jinja templates in a documentation

Project description

https://github.com/tardyp/sphinx-jinja/actions/workflows/ci.yml/badge.svg

sphinx-jinja

A sphinx extension to include jinja based templates based documentation into a sphinx doc

Usage

In your rst doc, you can use the following snippet to use a jinja template to generate your doc

.. jinja:: first_ctx

    {% for k, v in topics.items() %}

    {{k}}
    ~~~~~
    {{v}}
    {% endfor %}

In your sphinx conf.py file, you can create or load the contexts needed for your jinja templates

extensions = ['sphinxcontrib.jinja']

jinja_contexts = {
    'first_ctx': {'topics': {'a': 'b', 'c': 'd'}}
}

You can also customize the jinja Environment by passing custom kwargs, adding filters, tests, and globals, and setting policies:

jinja_env_kwargs = {
    'lstrip_blocks': True,
}

jinja_filters = {
    'bold': lambda value: f'**{value}**',
}

jinja_tests = {
    'instanceof': lambda value, type: isinstance(value, type),
}

jinja_globals = {
    'list': list,
}

jinja_policies = {
    'compiler.ascii_str': False,
}

Which can then be used in the templates:

Lists
-----

{% for o in objects -%}
    {%- if o is instanceof list -%}
        {%- for x in o -%}
            - {{ x|bold }}
        {% endfor -%}
    {%- endif -%}
{%- endfor %}

Available options

  • file: allow to specify a path to Jinja instead of writing it into the content of the directive. Path is relative to the current directory of sphinx-build tool, typically the directory where the conf.py file is located.

  • header_char: character to use for the the headers. You can use it in your template to set your own title character:

    For example:

    Title
    {{ options.header_char * 5 }}
  • header_update_levels: If set, a header in the template will appear as the same level as a header of the same style in the source document, equivalent to when you use the include directive. If not set, headers from the template will be in levels below whatever level is active in the source document.

  • debug: print debugging information during sphinx-build. This allows you to see the generated rst before sphinx builds it into another format.

Example of declaration in your RST file:

.. jinja:: approval_checks_api
   :file: relative/path/to/template.jinja
   :header_char: -

Each element of the jinja_contexts dictionary is a context dict for use in your jinja templates.

Running tests

  • pip install tox

  • tox

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinx-jinja-1.4.0.tar.gz (4.7 kB view details)

Uploaded Source

Built Distribution

sphinx_jinja-1.4.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file sphinx-jinja-1.4.0.tar.gz.

File metadata

  • Download URL: sphinx-jinja-1.4.0.tar.gz
  • Upload date:
  • Size: 4.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.9

File hashes

Hashes for sphinx-jinja-1.4.0.tar.gz
Algorithm Hash digest
SHA256 e6614d986c0289cb85b016c25eb8cb9781ceb841e70bee639c5123f39ad90b38
MD5 136a82bbd64aa3ddd61875c9d80954a0
BLAKE2b-256 12c12d232cbfcff4576f207904eb76c19b729de4fbcd73fed4f3a75fc9e9c655

See more details on using hashes here.

Provenance

File details

Details for the file sphinx_jinja-1.4.0-py3-none-any.whl.

File metadata

  • Download URL: sphinx_jinja-1.4.0-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.7.3 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.9

File hashes

Hashes for sphinx_jinja-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aa5ee594dad7c9cbce8240f39c39a975b9543ffc41f973b4e38f0fabd885f005
MD5 95c290b955141c4b341a686d837eb5c5
BLAKE2b-256 25047eceb00b519feb27477ff875d380215dcc62d529867ae22e130e1496d6a1

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page