53

我正在使用 Jekyll 作为网站(不是博客)的静态生成器,并且我希望在我的索引页面上自动生成所有页面的列表。具体来说,我希望有不同的类别并分别列出每个类别中的所有文章。是我所描述的一个示例,如果您在关注时遇到问题。Jekyll 有没有办法做到这一点(例如 GitHub 页面)?我已经看过变量文档页面,但这似乎特定于博客文章格式。

4

4 回答 4

89

在构建我自己的网站时,我遇到了同样的问题,我找到了一个(恕我直言)简单而强大的解决方案。希望这对其他希望做类似事情的人有用。

问题

给定网站上的页面子集(不是帖子),根据它们的类别将它们列在标题下。例如:给定一组我们认为是资源页面(或参考页面,或您想要显示的任何逻辑页面分组)的页面,我们希望将它们列在它们的类别下(例如代码、解释等) .

解决方案

为了得到我们想要的行为,我们必须在三个地方进行修改:

  • _config.yml
  • resources.md
  • resource-file-X.md

_config.yml

_config.yml中,我们必须添加将出现在资源文件中的所有类别/关键字/标签(或您想调用的任何名称)的列表。这是我的:

category-list: [code, editors, math, unix]

你可以调用任何变量,我选择了category-list,只要确保你在resource.md文件中使用相同的变量。

注意:您在列表中放置项目的顺序是它们将在resource.md页面上列出的顺序。

resource-file-X.md

这些是您希望在resources.md页面上建立索引和链接的文件。您需要做的就是在每个文件的顶部添加两个文件变量。首先是表明这个文件是一个资源文件。

resource: true

第二个是表明您希望该文件在哪些类别下被索引。您可以根据需要将其编入尽可能多的类别,如果您希望页面未编入索引,请将列表留空。我对 C 中正确 EINTR 处理的参考有以下几类:

categories: [code, unix]

resources.md

该文件将根据各自的类别生成页面列表。您需要做的就是将以下代码添加到此文件(或您希望列表所在的任何文件):

{% for cat in site.category-list %}
### {{ cat }}
<ul>
  {% for page in site.pages %}
    {% if page.resource == true %}
      {% for pc in page.categories %}
        {% if pc == cat %}
          <li><a href="{{ page.url }}">{{ page.title }}</a></li>
        {% endif %}   <!-- cat-match-p -->
      {% endfor %}  <!-- page-category -->
    {% endif %}   <!-- resource-p -->
  {% endfor %}  <!-- page -->
</ul>
{% endfor %}  <!-- cat -->

代码分解

简单解释一下这是如何工作的:

  • 循环遍历 中指定的每个类别_config.yml
  • 显示具有该类别名称的标题。
  • 为属于该类别的页面创建一个无序列表。
  • 循环浏览网站上的页面。
  • 如果页面是文件变量 指示的资源resource文件,则对于文件所属的每个类别,如果其中一个与列出的当前类别匹配,则显示指向该页面的链接。

注意:资源文件中的变量category-list可以_config.yml任意categories调用,只要确保在生成列表的文件中使用相同的变量即可。

另一个注意事项:修改_config.yml时,必须完全重启 Jekyll,即使有--watch选项,也必须停止并重启。我花了一段时间才弄清楚为什么我的更改没有生效!

最终产品

你可以在我网站的资源页面上看到最终产品,虽然我今天只是把它放在一起,所以在撰写本文时,它还远未完成,但你可以在主页上查看我的简历。

希望这可以帮助!

于 2013-07-28T21:44:42.810 回答
18

使用液体“包含”属性有一种更清洁的方法:

在 _config.yml 中,添加您的类别索引

categories: [fruit, meat, vegetable, cheese, drink]

在你的 page.md 里面的 front matter 中,添加 _config.yml 中可用的一个或多个类别

---
layout: page
title: Orange juice
description: Orange juice is juice from oranges. It's made by squeezing oranges.
categories: [fruit, drink]
---

在您的模板中获取您所做的水果类别中的所有页面:

{% for page in site.pages %}
  {% if page.categories contains 'fruit' %}
    <div class="item">
      <h3>{{page.title}}</h3>
      <p>{{page.description}}</p>
    </div>
  {% endif %}
{% endfor %}
于 2014-08-01T05:28:22.843 回答
2

You should differentiate between pages and posts (articles). Listing all posts sorted by category is not a problem at all. You can loop through site.categories. It contains the category name and a list of all posts in that category.

Listing all pages is possible, too. You can loop through site.pages. But a page does not belong to a specific category (only posts do).

When I take a look at your posted example, using categories on posts and then looping through site.categories seems to be the way to go. It will get you exactly the desired output.

于 2013-06-15T08:36:24.440 回答
1

可能有一些变化/简化(felipesk 的回答)。也许是由于 Jekyll 的改进。

中不需要索引_config.yml

如果页面列表未在页面中列出,但例如在文档中,您也可以将类别添加到文档中:

---
layout: doc
title: Fruit List
categories: [fruit]
---

然后像这样使用它:

{% for p in site.pages %}
   {% if p.categories contains page.category %}
     * [{{ p.title }}]({{ p.url | absolute_url }})
        <small>{{ p.excerpt }}</small>
   {% endif %}
{% endfor %}

有了帖子,这甚至可以更短:

{% for post in site.categories[page.category] %}
  * [{{ post.title }}]({{ post.url | absolute_url }})  
      <small>{{ post.excerpt }}</small>
{% endfor %}

为什么这只适用于帖子,我还不明白。

有趣的是,这个片段可以在任何地方使用(如果你混合使用 docs/pages/posts)!所以只需将其添加为_includes并使用它:

## Further Reading
{% include pages-list.md %}

我以最小的错误为主题

于 2018-03-16T13:56:44.453 回答