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 |
---|---|
|
The name of your collection, e.g. |
|
An array of documents. |
|
An array of static files in the collection. |
|
The path to the collection's source directory, relative to the site source. |
|
The full path to the collections's source directory. |
|
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 |
---|---|
|
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. |
|
The rendered output of the document, based on the |
|
The full path to the document's source file. |
|
The path to the document's source file relative to the site source. |
|
The URL of the rendered collection.
The file is only written to the destination when the collection to which it belongs has |
|
The name of the document's collection. |
|
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
and you process the collections using the default ordering
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
-
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.
-
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).
Sort a collection in reverse order
Sorting a Jekyll collection in reverse order (latest collection post first) is pretty straightforward in Jekyll.
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.
If you leave out | reverse
, your collection will show up oldest post first.