Search code examples
pythonhtmldocumentationpython-sphinx

How to modify html title separator in Sphinx doc generator


By default it seems to what to use a long dash '--' as the separator between page title and overall site html_title that's set in the config.py file.

We'd like to change this to a '|' character instead.

I can add a block to the layout.html template to modify the title I'm just unsure of what to actually write for that. I want it to be 'page_title | html_title' in the title tags across the site.


Solution

  • The em dash (—) comes from the layout.html template:

    {%- if not embedded and docstitle %}
    {%- set titlesuffix = " — "|safe + docstitle|e %}
    {%- else %}
    {%- set titlesuffix = "" %}
    {%- endif %}
    

    The value of titlesuffix is used a bit further down in the template:

    {%- block htmltitle %}
    <title>{{ title|striptags|e }}{{ titlesuffix }}</title>
    {%- endblock %}
    

    To customize:

    1. Ensure that you have templates_path = ['_templates'] in conf.py.

    2. Create a file called layout.html in the _templates directory.

    3. In layout.html, add the following:

      {% extends "!layout.html" %}
      
      {%- set customtitlesuffix = " | "|safe + docstitle|e %}
      
      {%- block htmltitle %}
      <title>{{ title|striptags|e }}{{ customtitlesuffix }}</title>
      {%- endblock %}
      

    See also https://www.sphinx-doc.org/en/master/templating.html.