问题描述
我正在从 sphinx-doc ,我注意到基本主题layout.html
script
宏
I'm extending the basic theme from sphinx-doc and I notice the following code chunk inside basic theme layout.html
script
macro
{%- for scriptfile in script_files %}
<script type="text/javascript" src="{{ pathto(scriptfile, 1) }}"></script>
{%- endfor %}
这是否意味着我可以在theme.conf
中添加如下所示的html_theme_options
:
Does that mean I can add a html_theme_options
like the following inside my theme.conf
:
[options]
script_files =
,然后在我的conf.py
中添加:
html_theme_options = {'script_files': '_static'}
但是,使用此设置,构建完全被搞砸了,并产生了如下垃圾页面:
However, with this set, the build is totally messed up and produced junk pages like:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>...</head>
<body>
-->
<!-- my js code but is automatically commented out-->
</body>
</html>
哪个部分出了问题?我应该怎么做才能加载自己的自定义javascript?非常感谢!
Which part goes wrong? What should I do in order to load my own customized javascript? Thanks much!
推荐答案
script_files
是模板内部变量.您无法通过html_theme_options
进行设置(所有主题变量均以theme_
作为前缀,请参见下文).
script_files
is a template-internal variable. You cannot set it via html_theme_options
(all theme variables have theme_
as a prefix, see below).
Sphinx文档在此处中说明了如何添加其他脚本通过script_files
变量直接在模板文件中.
The Sphinx docs explain here how to add additional scripts directly in the template files, via the script_files
variable.
如果您认为在conf.py
中定义其他脚本很重要,请按照以下步骤操作:
In case you regard it as important to define the additional scripts in your conf.py
, proceed as follows:
-
将以下行添加到模板的
layout.html
中,例如,在DOCTYPE
定义的endblock
下面:
Add the following line to your template's
layout.html
, for example below theendblock
of theDOCTYPE
definition:
{% set script_files = script_files + theme_extra_scripts %}
在theme.conf
中定义主题变量extra_scripts
及其默认值:
Define the theme variable extra_scripts
and its default value in theme.conf
:
extra_scripts = []
覆盖conf.py
中的变量:
html_theme_options = {
'extra_scripts': ['_static/test.js']
}
这篇关于如何在Sphinx-doc中加载外部JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!