pythonjinja2python-sphinx

Finding variables passed to jinja2 from sphinx


I am trying to create a template in sphinx. My intention is not to use the basic template, but to build a new one from scratch. However there is very little (/no) documentation about what variables are passed from sphinx into the templates.

I would like to dump all of the variables that are being passed to the template from sphinx. (ideally within a template as my python knowledge isn't good!)


Solution

  • The Sphinx documentation provides a list of most variables that are available in templates. You find additional variables - of which most are passed on by docutils - in this dictionary (Sphinx source code).

    In addition, you can use the html_theme_options dictionary in the conf.py file to add custom configuration variables to your own template.

    The custom configuration variables need to be accessed with the prefix theme_.

    For example, if you define your custom variables as follows:

    html_theme_options = {
        'pdf_path': '/docs/MyDocs.pdf'
    }
    

    , you can access the pdf_path variable as {{theme_pdf_path}}.

    Alternatively, the custom theme variables can be defined in your theme's theme.conf file. In our example, you would add the line pdf_path = /docs/MyDocs.pdf.