Jekyll For Loop 包含一个目录的所有内容

Jekyll For Loop to include all of a directory

我正在使用 Jekyll 在 GitHub 上创建静态页面,我已经设置了我的默认布局和 index.html 页面。

我想创建一个名为 'sections' 的文件夹并存储大量 .html 文件,这些文件基本上是我正在制作的指南的不同部分。这样做的目的是让我可以使用 Jekyll for 循环将整个 'sections' 包含到我的主 index.html 中。我想将我的所有部分都保留在部分文件夹中,以使其更易于管理。

这可能吗?

Jekyll 的目录结构不是你可以随心所欲改变的。如果您希望每个 html 页面在您的网站中作为单独的页面,您需要将 html 页面放入项目的 _posts 目录中并使用 for 像这样循环将它们的链接包含在 index.html:

{% for post in site.posts %}
    <!-- link to each post in index.html -->
    <a href="{{ site.url }}{{ post.url }}">{{ post.title }}</a>
{% endfor %}

如果您想将每个 html 文件的内容放入 index.html,那么您不能为此使用 _posts。在这种情况下,您需要使用 _includes 目录,并执行如下操作:

... 
{% include page1.html %}
{% include page2.html %}
{% include page3.html %}
...

参见Jekyll directory structure doc