详细教程(分步骤)

9. Collections:集合(专题页面、聚合页面)

  1. Setup:安装、创建和构建 Jekyll 的详细教程
  2. Liquid:模板语言
  3. Front Matter:页面头部参数
  4. Layouts:布局
  5. Includes:引入文件、包含文件、可复用片段、模板片段
  6. Data Files:数据文件
  7. Assets:资源文件(css、js、图片)
  8. Blogging:Jekyll 博客系统
  9. Collections:集合(专题页面、聚合页面)
  10. Deployment:部署

让我们来完善作者页面,让每位作者都有自己的专属页面,包含简介和他们发布的文章。

为此,你将使用 collections。Collections 类似于 posts,但它们的内容不需要按日期分组。

配置

要设置 collection,你需要告诉 Jekyll 相关信息。Jekyll 的配置默认存储在 _config.yml 文件中。

在项目根目录下创建 _config.yml,并添加以下内容:

collections:
  authors:

要重新加载配置,需要重启 Jekyll 服务器。在终端中按 Ctrl+C 停止服务器,然后运行 jekyll serve 重新启动。

添加作者

文档(collection 中的项目)存放在站点根目录的 _ *collection_name* 文件夹中。本例中,使用 _authors

为每位作者创建一个文档:

_authors/jill.md:

---
short_name: jill
name: Jill Smith
position: Chief Editor
---
Jill 是一位热衷于种植水果的农夫,常驻法国南部。

_authors/ted.md:

---
short_name: ted
name: Ted Doe
position: Writer
---
Ted 从婴儿时期就开始吃水果了。

团队页面

接下来,添加一个页面,列出站点上的所有作者。Jekyll 会将 collection 公开为 site.authors

创建 staff.html,遍历 site.authors 以输出所有团队成员:

---
layout: default
title: Staff
---
<h1>Staff</h1>

<ul>
  {% for author in site.authors %}
    <li>
      <h2>{{ author.name }}</h2>
      <h3>{{ author.position }}</h3>
      <p>{{ author.content | markdownify }}</p>
    </li>
  {% endfor %}
</ul>

由于内容是 Markdown 格式,你需要使用 markdownify 过滤器进行解析。当在布局中使用 {{ content }} 输出时,这个转换会自动进行。

你还需要在主导航中添加前往该页面的入口。打开 _data/navigation.yml,添加团队页面的条目:

- name: Home
  link: /
- name: About
  link: /about.html
- name: Blog
  link: /blog.html
- name: Staff
  link: /staff.html

+

输出页面

默认情况下,collections 不会为文档生成单独的页面。在本例中,我们希望每位作者都有自己的页面,因此需要调整 collection 的配置。

打开 _config.yml,在 authors collection 配置中添加 output: true

collections:
  authors:
    output: true

重新启动 Jekyll 服务器,使配置更改生效。

你可以使用 author.url 链接到生成的作者页面。

修改 staff.html,添加链接:

---
layout: default
title: Staff
---
<h1>Staff</h1>

<ul>
  {% for author in site.authors %}
    <li>
      <h2><a href="{{ author.url }}">{{ author.name }}</a></h2>
      <h3>{{ author.position }}</h3>
      <p>{{ author.content | markdownify }}</p>
    </li>
  {% endfor %}
</ul>

和文章一样,你需要为作者创建一个布局文件。

创建 _layouts/author.html,内容如下:

---
layout: default
---
<h1>{{ page.name }}</h1>
<h2>{{ page.position }}</h2>

{{ content }}

Front matter(页面头部参数)默认值

现在,你需要让所有作者文档默认使用 author 布局。你可以像之前一样在 front matter 中手动指定,但这样会显得重复。

理想情况下,所有文章默认使用 post 布局,作者页面使用 author 布局,其他页面使用 default 布局。

你可以在 _config.yml 中使用 front matter(页面头部参数)默认值 实现这一点。你需要设置一个作用范围(scope),然后定义默认的 front matter 值。

_config.yml 中添加默认布局设置:

collections:
  authors:
    output: true

defaults:
  - scope:
      path: ""
      type: "authors"
    values:
      layout: "author"
  - scope:
      path: ""
      type: "posts"
    values:
      layout: "post"
  - scope:
      path: ""
    values:
      layout: "default"

现在,你可以从所有页面和文章的 front matter 中移除 layout 选项。请注意,每次更新 _config.yml 后,都需要重新启动 Jekyll 才能使更改生效。

列出作者的文章

在作者页面上列出该作者发布的文章。要实现这一点,需要匹配作者的 short_name 与文章的 author,然后使用这个匹配结果来筛选文章。

_layouts/author.html 中遍历筛选后的文章列表,输出该作者的文章:

---
layout: default
---
<h1>{{ page.name }}</h1>
<h2>{{ page.position }}</h2>

{{ content }}

<h2>Posts</h2>
<ul>
  {% assign filtered_posts = site.posts | where: 'author', page.short_name %}
  {% for post in filtered_posts %}
    <li><a href="{{ post.url }}">{{ post.title }}</a></li>
  {% endfor %}
</ul>

文章已经包含作者信息,现在我们需要将作者名链接到对应的作者页面。在 _layouts/post.html 中使用类似的筛选方法:

---
layout: default
---
<h1>{{ page.title }}</h1>

<p>
  {{ page.date | date_to_string }}
  {% assign author = site.authors | where: 'short_name', page.author | first %}
  {% if author %}
    - <a href="{{ author.url }}">{{ author.name }}</a>
  {% endif %}
</p>

{{ content }}

打开 http://localhost:4000,查看 staff 页面以及文章中的作者链接,确保所有内容正确关联。

在本教程的最后一步,我们将对站点进行优化,使其准备好用于生产环境。

  1. Setup:安装、创建和构建 Jekyll 的详细教程
  2. Liquid:模板语言
  3. Front Matter:页面头部参数
  4. Layouts:布局
  5. Includes:引入文件、包含文件、可复用片段、模板片段
  6. Data Files:数据文件
  7. Assets:资源文件(css、js、图片)
  8. Blogging:Jekyll 博客系统
  9. Collections:集合(专题页面、聚合页面)
  10. Deployment:部署