UP

Collections are a great way to group related content.

Liquid Attributes

Collections

Collections are available under site.collections,
with the metadata you specified in your _config.yml (if present) and the following information:

这里是针对每一个collection而言,它的attribute有两部分组成,第1部分是由_config.yml定义,第2部分是以下定义的属性。

Variable Description

label

The name of your collection, e.g. my_collection.

docs

An array of documents.

files

An array of static files in the collection.

relative_directory

The path to the collection's source directory, relative to the site source.

directory

The full path to the collections's source directory.

output

Whether the collection's documents will be output as individual files.

Documents

In addition to any front matter provided in the document’s corresponding file,
each document has the following attributes:

这里是针对每一个collection里面的document而言,它的attribute有两部分组成,第1部分是由front matter定义,第2部分是以下定义的属性。

Variable Description

content

The (unrendered) content of the document. If no front matter is provided, Jekyll will not generate the file in your collection. If front matter is used, then this is all the contents of the file after the terminating `---` of the front matter.

output

The rendered output of the document, based on the content.

path

The full path to the document's source file.

relative_path

The path to the document's source file relative to the site source.

url

The URL of the rendered collection. The file is only written to the destination when the collection to which it belongs has output: true in the site's configuration.

collection

The name of the document's collection.

date

The date of the document's collection.

Iterate collections

Somewhat annoyingly, Jekyll’s default list of collections is in alphabetical order of collection name.

First Example

So if your _config.yml specifies

collections:
  moose_and_goose_stories:
    title: The Moose and Goose Stories
  grey_parrot_stories:
    title: The Grey Parrot Stories
  predicting_the_present:
    title: Predicting the Present

and you process the collections using the default ordering

{% for item in site.collections %}
- label: {{ item.label }}
  - directory: {{ item.directory }}
  - relative_directory: {{ item.relative_directory }}
  - output: {{ item.output }}
{% endfor %}

it will give you

  • label: grey_parrot_stories
    • directory: D:/git-repo/myblog/_grey_parrot_stories
    • relative_directory: _grey_parrot_stories
    • output: false
  • label: moose_and_goose_stories
    • directory: D:/git-repo/myblog/_moose_and_goose_stories
    • relative_directory: _moose_and_goose_stories
    • output: false
  • label: posts
    • directory: D:/git-repo/myblog/_posts
    • relative_directory: _posts
    • output: true
  • label: predicting_the_present
    • directory: D:/git-repo/myblog/_predicting_the_present
    • relative_directory: _predicting_the_present
    • output: false

Second Example

{% for item in site.collections %}
  {% if item.title %}
- {{ item.title | escape }}
  {% endif %}
{% endfor %}
  • The Grey Parrot Stories

  • The Moose and Goose Stories

  • Predicting the Present

Third Example

Modify the _config.yml file, and add a sequence attribute to each collection.

collections:
  moose_and_goose_stories:
    title: The Moose and Goose Stories
    sequence: 1
  grey_parrot_stories:
    title: The Grey Parrot Stories
    sequence: 2
  predicting_the_present:
    title: Predicting the Present
    sequence: 3
{% assign collections = site.collections | sort: "sequence" %}
{% for item in collections %}
  {% if item.title %}
- {{ item.title | escape }}
  {% endif %}
{% endfor %}
  • The Moose and Goose Stories

  • The Grey Parrot Stories

  • Predicting the Present

Sort documents in a collection

For the sake of this example, let’s assume we have a collection called projects.

Sort a collection in ascending order

Here is how we can get a collection posts in ascending order on a page or index (oldest collection post first).

{% assign projects = site.projects %}

{% for project in projects %}

Sort a collection in reverse order

Sorting a Jekyll collection in reverse order (latest collection post first) is pretty straightforward in Jekyll.

{% assign projects = site.projects %}

{% for project in projects reversed %}

Sort a collection in reverse order and limit the number of results

Now let’s look at how we can limit the number of posts from a collection and sort the result in descending order (latest collection post first). This is useful if you have a site with hundred of articles and want to create an index with the latest results in a given collection.

{% assign sorted = site.projects | sort: 'date' | reverse  %}

{% for project in sorted limit: 12 %}

If you leave out | reverse, your collection will show up oldest post first.

References