templates.rst 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. .. _templates:
  2. =========
  3. Templates
  4. =========
  5. ``sphinx-multiversion`` does not change the look of your HTML output by default.
  6. Instead, you can customize the template to cater to your needs.
  7. Version Listings
  8. ================
  9. To add version listings to your template, you need to add a custom template to your theme.
  10. You can take one of the snippets below, put it into :file:`_templates/versioning.html` and add it to your theme's sidebar:
  11. .. code-block:: html
  12. templates_path = [
  13. "_templates",
  14. ]
  15. html_sidebars = [
  16. "versioning.html",
  17. ]
  18. List all branches/tags
  19. ----------------------
  20. .. code-block:: html
  21. {% if versions %}
  22. <h3>{{ _('Versions') }}</h3>
  23. <ul>
  24. {%- for item in versions %}
  25. <li><a href="{{ item.url }}">{{ item.name }}</a></li>
  26. {%- endfor %}
  27. </ul>
  28. {% endif %}
  29. List branches and tags separately
  30. ---------------------------------
  31. .. code-block:: html
  32. {% if versions %}
  33. <h3>{{ _('Branches') }}</h3>
  34. <ul>
  35. {%- for item in versions.branches %}
  36. <li><a href="{{ item.url }}">{{ item.name }}</a></li>
  37. {%- endfor %}
  38. </ul>
  39. <h3>{{ _('Tags') }}</h3>
  40. <ul>
  41. {%- for item in versions.tags %}
  42. <li><a href="{{ item.url }}">{{ item.name }}</a></li>
  43. {%- endfor %}
  44. </ul>
  45. {% endif %}
  46. List releases and development versions separately
  47. -------------------------------------------------
  48. .. code-block:: html
  49. {% if versions %}
  50. <h3>{{ _('Releases') }}</h3>
  51. <ul>
  52. {%- for item in versions.releases %}
  53. <li><a href="{{ item.url }}">{{ item.name }}</a></li>
  54. {%- endfor %}
  55. </ul>
  56. <h3>{{ _('In Development') }}</h3>
  57. <ul>
  58. {%- for item in versions.in_development %}
  59. <li><a href="{{ item.url }}">{{ item.name }}</a></li>
  60. {%- endfor %}
  61. </ul>
  62. {% endif %}
  63. Version Banners
  64. ===============
  65. You can also add version banners to your theme, for example create a template file page.html in the templates directory:
  66. .. code-block:: html
  67. {% extends "!page.html" %}
  68. {% block body %}
  69. {% if current_version and latest_version and current_version != latest_version %}
  70. <p>
  71. <strong>
  72. {% if current_version.is_released %}
  73. You're reading an old version of this documentation.
  74. If you want up-to-date information, please have a look at <a href="{{ vpathto(latest_version.name) }}">{{latest_version.name}}</a>.
  75. {% else %}
  76. You're reading the documentation for a development version.
  77. For the latest released version, please have a look at <a href="{{ vpathto(latest_version.name) }}">{{latest_version.name}}</a>.
  78. {% endif %}
  79. </strong>
  80. </p>
  81. {% endif %}
  82. {{ super() }}
  83. {% endblock %}%
  84. ReadTheDocs Theme
  85. =================
  86. As of version 0.4.3, the `Read the Docs theme <sphinx_rtd_theme_>`_ does not support sidebar widgets.
  87. So instead of adding a custom template to ``html_sidebars``, you need to create a template file named :file:`versions.html` with the following content:
  88. .. code-block:: html
  89. {%- if current_version %}
  90. <div class="rst-versions" data-toggle="rst-versions" role="note" aria-label="versions">
  91. <span class="rst-current-version" data-toggle="rst-current-version">
  92. <span class="fa fa-book"> Other Versions</span>
  93. v: {{ current_version.name }}
  94. <span class="fa fa-caret-down"></span>
  95. </span>
  96. <div class="rst-other-versions">
  97. {%- if versions.tags %}
  98. <dl>
  99. <dt>Tags</dt>
  100. {%- for item in versions.tags %}
  101. <dd><a href="{{ item.url }}">{{ item.name }}</a></dd>
  102. {%- endfor %}
  103. </dl>
  104. {%- endif %}
  105. {%- if versions.branches %}
  106. <dl>
  107. <dt>Branches</dt>
  108. {%- for item in versions.branches %}
  109. <dd><a href="{{ item.url }}">{{ item.name }}</a></dd>
  110. {%- endfor %}
  111. </dl>
  112. {%- endif %}
  113. </div>
  114. </div>
  115. {%- endif %}
  116. .. _sphinx_rtd_theme: https://pypi.org/project/sphinx-rtd-theme/