Bud P. Bruegger
Bud P. Bruegger

Reputation: 1091

How to use config values in templates?

I set a config value big_logo with a simple extension. I also tested my way of setting config values with sphinx.ext.ifconfig and it seems to work.

Now, I'd like to use that in a template for layout.html as follows:

{% extends "!layout.html" %}
{% block header %}
    {%- if big_logo %}
       <div style="background-color: white;>
           <img src="{{ pathto("_static/" + big_logo, 1) }}" alt="logo" />
       </div>
    {% endif %}
{% endblock %}

This doesn't work, however; make html yields:

Exception occurred:
File "...", line 7, in block "header"
   <img src="{{ pathto("_static/" + big_logo, 1) }}" alt="logo" />
UndefinedError: 'big_logo' is undefined

This seems to be the same as the layout.html of the basic theme:

{%- block sidebarlogo %}
  {%- if logo %}
   <p class="logo"><a href="{{ pathto(master_doc) }}">
          <img class="logo" src="{{ pathto('_static/' + logo, 1) }}" alt="Logo"/>

My only doubt is that conf.py doesn't set the variable "logo" but "html_logo".

Any ideas how to use configuration values in templates?

Upvotes: 3

Views: 1720

Answers (1)

Kevin Horn
Kevin Horn

Reputation: 4295

You want to pass the config value the html context setting. From my understanding, only variables defined by Sphinx and variables in this dictionary will be available in the templates. I'm not sure exactly how this will interact with the way you are setting the config variable, assuming you want to do it like you describe in your other question, but it should be do-able. Just remember, the conf.py file is really just Plain Old Python.

Upvotes: 2

Related Questions