在 Jekyll 中列出所有 Collections

Listing all Collections in Jekyll

我正在使用 Jekyll (v2.5.3) 构建一个杂志站点。 the Jekyll site 上的文档让我相信我可以在我的网站上列出所有 collection,并在我的 _config.yml.[=15 中为每个 collection 嵌入 YAML 数据=]

_config.yml:

collections:
  issue_001:
    output: true
    permalink: /:title/:path
    title: Rebirth
    date: 2015-07-01
  issue_002:
    output: true
    permalink: /:title/:path
    title: Talking Heads
    date: 2015-08-01

index.html:

{% for issue in site.collections %}
  <li>
    <h6 class="post-meta">Issue {{ issue.name }} &mdash; {{ issue.date | date: "%b %-d, %Y" }}</h6>

    <h2>
      {{ issue.title }}
    </h2>
  </li>
{% endfor %}

我在主页上出现了两个问题,但是我正在为每个问题访问的数据(名称、日期、标题等)的 none 出现了。我很欣赏这是一个测试版功能,所以只是想问一下这是坏了,还是我做错了?

{% for issue in site.collections %}中,issue是一个包含:

的数组
0 => "issue_001",
1 => Hash
 {"output"=>true,
  "permalink"=>"/:title/:path",
  "title"=>"Rebirth",
  "date"=>#,
  "label"=>"issue_001",
  "docs"=>[#],
  "files"=>[],
  "directory"=>"/home/djacquel/www/test.dev/jekyll/wat/_issue_001",
  "relative_directory"=>"_issue_001"}

访问数据的正确方法是:

{% for issue in site.collections %}
  <li>
    <h6 class="post-meta">
      Issue {{ issue[1].label }}
      &mdash;
      {{ issue[1].date | date: "%b %-d, %Y" }}
    </h6>
    <h2>
      {{ issue[1].title }}
    </h2>
  </li>
{% endfor %}

如果有人仍然有这个请求,这里是它在最新的 Jekyll (v3.8.3) 中的工作方式:

更新:测试它在 v4.2.0 中仍然有效

{% for collection in site.collections %}
  <h2>Items from {{ collection.label }}</h2>
  <ul>
    {% for item in site[collection.label] %}
      <li><a href="{{ item.url }}">{{ item.title }}</a></li>
    {% endfor %}
  </ul>
{% endfor %}

第二个第一; answer from @sparanoid 太棒了!在它和默认的 Jekyll 主题 (Minima) 的源代码之间,我能够避开下面的一些内容。

第一秒;很好的问题,两者都不是 up-voted 有点莫名其妙。

以及许多其他相关点中的第三个;我与问题海报的路径不同,但以下代码可能对读者有用。我的用例想要一种列出类似于默认 Jekyll home 布局的 collection 节制的方法。

_layouts/named_collection.html

Note the source links are downloadable via, clicking on the Raw link/button then Ctrl s to save as a regular text file, however, these links have been frozen in time so be sure to check the gh-pages branch for updates. And output examples are intended in this case to be just examples of possible content, eg. it could be Lorem strings for all that it matters to demonstrate what's to follow.

---
layout: default
---
{% comment %}
License note from S0AndS0; an editor in this context.

Minima (where the following code is sourced from), is shared under the
 MIT Lisense, https://github.com/jekyll/minima/blob/master/LICENSE.txt

Thus any alterations to Minima source code is re-shared under the MIT Lisense
{% endcomment %}
{% capture workspace_collections %}
  {% assign collection_name = page.collection_name | default: include.collection_name | default: nil %}
  {% assign target_collection = site[collection_name] %}

  <div class="home">
    {%- if page.title -%}
      <h1 class="page-heading">{{ page.title }}</h1>
    {%- endif -%}

    {{ content }}

    {% assign has_output = False %}
    {%- if target_collection.size > 0 -%}
      {% capture workspace_collection %}
        <h2 class="post-list-heading">{{ page.list_title | default: "Posts" }}</h2>
        <ul class="post-list">
          {%- for post in target_collection -%}
            {%- if page.relative_path == post.relative_path -%}
              {%- continue -%}
            {%- else -%}
              {% assign has_output = True %}
            {%- endif -%}

            <li>
              {%- assign date_format = site.minima.date_format | default: "%b %-d, %Y" -%}
              <span class="post-meta">{{ post.date | date: date_format }}</span>
              <h3>
                <a class="post-link" href="{{ post.url | relative_url }}">
                  {{ post.title | escape }}
                </a>
              </h3>
              {%- if site.show_excerpts -%}
                {{ post.excerpt | markdownify | remove: '<p>' | remove: '</p>' }}
              {%- endif -%}
            </li>
          {%- endfor -%}
        </ul>
      {% endcapture %}

      {%- if has_output == True -%}
        {{- workspace_collection -}}{% assign workspace_collection = nil %}
      {%- endif -%}

    {%- endif -%}
  </div>
{% endcapture %}{{ workspace_collections | strip }}{% assign workspace_collections = nil %}

Side note, I can't remember where I picked up that capture some workspace_ trick, but it's pretty sweat for stripping new lines and other formatting shenanigans. If a reader figures out who started such fanciness, please edit or leave a comment.


此布局的用法与 Minima 的 home.html 布局的用法非常相似。

administration.md

...Front-Matter 从 _administration 中选择 collection 看起来像...

layout: named_collection
collection_name: administration
title: Administration
list_title: Usage Examples
permalink: /admin/

... 导致输出 <base-url>/admin/ collection 列表。

git_shell_commands.md

...Front-Matter 从 _git_shell_commands 中选择 collection 看起来像...

layout: named_collection
collection_name: git_shell_commands
title: Git Shell Commands
list_title: Usage Examples
permalink: /git_shell_commands/

... 导致输出 <base-url>/git_shell_commands/ collection 列表。


关于 collections

的更多注释

如果我正确阅读了最新的 Jekyll Collections 文档,从版本 3.7.0 开始,在这个问题的情况下,可能会有一个自定义的 collections 目录可能看起来像...

ls ~/git/magazine-name/
# ... other files and/or directories
#   issues/
# ... other files and/or directories
#   _config.yml

... 其中 issues/ 目录包含 sub-directories 例如...

ls ~/git/magazine-name/issues/
#   _001/
#   _002/
#   _003/
# ...

... _config.yml 文件有...

collections_dir: issues
collections:
  001:
    output: True
    # ...

... magic saucecollections_dir 不是 使用 under-scored 前缀基目录。关于最后一点,文档大声,例如。 _issues/ 会造成 糟糕的时间

我不确定是否需要对之前发布的 Liquid 代码进行任何其他调整(我不这么认为,但可能如此);所以也许先单独尝试一下。

Warning/Update

Jekyll does not take kindly to numbered collections, eg. _000 will cause errors! To mitigate such issues be sure to trick Jekyll into treating collection names as strings, eg. _i000

我建议使用 collections_dir 的原因是,在 @James Dinsdale's question 的情况下,它可能有助于更好地组织事情。我个人希望我的 gh-pages 分支 collection 到 镜像 与我项目的 master 分支有一些相同的路径,所以没有进行测试 collections_dir 个建议。


关于我的 Liquid 编码选择的注意事项

因为我使用的是 Minima 主题,所以我将使用 named_collection 主题的文件放在项目的根目录中;默认行为是 link 到每个页面顶部的 pages。便宜的导航。

因为读者可能会将使用 named_collection 的页面放置在与所列内容相同的目录中,例如...

ls Jekyll_Admin/_administration/
#   _administration/administration.md
# ...

...我已经为一些 edge-cases 编写了 Liquid,但可能没有涵盖所有内容。在生产中释放类似的东西之前,一定要私下测试。


更新

在使用 collections_dir 进行一些测试后,看起来在使用 _posts 目录时 也必须 移动到该目录,例如...

_config.yml(截图)

collections_dir: misc

Bash 命令

mkdir misc
mv _posts misc/

...虽然这仅适用于使用 _posts 目录。


从上次编辑到您正在阅读本文,我已经在 GitHub 上发布了 liquid-utilities/collection-home;可能对那些想要版本跟踪解决方案的人有用,这也是我目前在需要此功能的任何项目中使用的。