我应该如何将分配的变量传递给包含的 Jekyll 文件?
How should I pass assigned variables to an included Jekyll file?
我使用 github 个页面来托管一个 jekyll 网站。为了在在线基础 URL 和本地基础 URL 之间快速切换,我在默认布局文件的开头有一个变量:
<!--{% assign root = site.github.url %}-->
{% assign root = "." %}
但是,如果我尝试在包含的文件中使用该变量,它不起作用。我知道它应该被称为 {{ include.root }}
,到目前为止我尝试了几种变体,但 none 有效:
{% include rail.html root = root %}
{% include rail.html root = page.root %}
{% include rail.html root = {{ root }} %}
知道我应该做什么吗?
请注意,我使用 default.html
文件作为布局从索引文件中调用包含。我可以在该索引文件中正确打印 root
变量。
此外,如何共享父数据上下文而不必将所有参数传递到我包含的文件?
编辑:我好像搞错了。我试图将内联帖子放入包含中,并且数据上下文被完美共享。不知道为什么它只在 includes
中不适用于我的根变量
包含标签的语法如下:{% include file.ext param='value' param2='value' %}
{% include rail.html root = page.root %}
不起作用,因为 root
未定义为 page
.
的属性
{% include rail.html root = {{ root }} %}
语法错误,你必须从源代码中删除整行,否则会使 build
失败。
第一个正确:{% include rail.html root = root %}
然后在 _includes/rail.html
中:{{ include.root }}
应该打印 root
值,如果这不起作用错误在另一个地方,而不是 include.
旁注
如果在include之前定义变量,那么可以不用include.
直接使用:
{% assign root = "." %}
{% include rail.html %}
在_includes/rail.html
中:
Hello {{root}}
我使用 github 个页面来托管一个 jekyll 网站。为了在在线基础 URL 和本地基础 URL 之间快速切换,我在默认布局文件的开头有一个变量:
<!--{% assign root = site.github.url %}-->
{% assign root = "." %}
但是,如果我尝试在包含的文件中使用该变量,它不起作用。我知道它应该被称为 {{ include.root }}
,到目前为止我尝试了几种变体,但 none 有效:
{% include rail.html root = root %}
{% include rail.html root = page.root %}
{% include rail.html root = {{ root }} %}
知道我应该做什么吗?
请注意,我使用 default.html
文件作为布局从索引文件中调用包含。我可以在该索引文件中正确打印 root
变量。
此外,如何共享父数据上下文而不必将所有参数传递到我包含的文件?
编辑:我好像搞错了。我试图将内联帖子放入包含中,并且数据上下文被完美共享。不知道为什么它只在 includes
中不适用于我的根变量包含标签的语法如下:{% include file.ext param='value' param2='value' %}
{% include rail.html root = page.root %}
不起作用,因为root
未定义为page
. 的属性
{% include rail.html root = {{ root }} %}
语法错误,你必须从源代码中删除整行,否则会使build
失败。第一个正确:
{% include rail.html root = root %}
然后在_includes/rail.html
中:{{ include.root }}
应该打印root
值,如果这不起作用错误在另一个地方,而不是 include.
旁注
如果在include之前定义变量,那么可以不用include.
直接使用:
{% assign root = "." %}
{% include rail.html %}
在_includes/rail.html
中:
Hello {{root}}