Sphinx - 使用自定义 Jinja2 ChoiceLoader 以特定顺序加载模板

Sphinx - use custom Jinja2 ChoiceLoader to load templates in specific order

我的问题是我需要从我在其他存储库和应用程序中共享的 python 包加载 jinja2 模板。我的本地 sphinx 主题模板将扩展这些共享模板。

在烧瓶中我做了这样的事情

# Load common templates
template_loader = jinja2.ChoiceLoader([
    app.jinja_loader,
    jinja2.PackageLoader('my_shared_templates', 'templates')
])
app.jinja_loader = template_loader

我在 sphinx 中尝试做类似的事情时遇到了很多麻烦。我一直在研究编写一个 sphinx 扩展来实现这一点,但没有任何进展。我希望有人有足够的 Sphinx 经验,他们可以为我指明正确的方向以了解如何做到这一点。谢谢。

事实证明,使用 sphinx 扩展是解决此问题的倒退方法。我最终得到了一个更简单的解决方案。 在我的 conf.py 文件中,我刚刚将包添加到 templates_path 设置中。

import my_shared_templates
import os
path = os.path.dirname(my_shared_templates.__file__)
path = path+'/templates'
# Add any paths that contain templates here, relative to this directory.
templates_path = [path, '_templates']

我在 /_templates 中的模板继承自我在 my_shared_templates 中的模板。