---
title: Collections
permalink: /docs/collections/
---
Collections are a great way to group related content like members of a team or
talks at a conference.
## Setup
To use a Collection you first need to define it in your `_config.yml`. For
example here's a collection of staff members:
```yaml
collections:
- staff_members
```
In this case `collections` is defined as a sequence (i.e., array) with no additional metadata defined for each collection.
You can optionally specify metadata for your collection by defining `collections` as a mapping (i.e., hashmap) instead of sequence, and then defining additional fields in it:
```yaml
collections:
staff_members:
people: true
```
{: .note .info}
When defining a collection as a sequence, its pages will not be rendered by
default. To enable this, output: true
must be specified on the
collection, which requires defining the collection as a mapping. For more
information, see the section Output.
You can optionally specify a directory to store all your collections in the same place with collections_dir: my_collections
.
Then Jekyll will look in my_collections/_books
for the books
collection, and
in my_collections/_recipes
for the recipes
collection.
If you specify a directory to store all your collections in the same place with collections_dir: my_collections
, then you will need to move your _drafts
and _posts
directory to my_collections/_drafts
and my_collections/_posts
. Note that, the name of your collections directory cannot start with an underscore (`_`).
The folder must be named identically to the collection you defined in
your _config.yml
file, with the addition of the preceding _
character.
{{ staff_member.content | markdownify }}
{% endfor %} ``` {% endraw %} If you'd like Jekyll to create a rendered page for each document in your collection, you can set the `output` key to `true` in your collection metadata in `_config.yml`: ```yaml collections: staff_members: output: true ``` You can link to the generated page using the `url` attribute: {% raw %} ```liquid {% for staff_member in site.staff_members %}{{ staff_member.content | markdownify }}
{% endfor %} ``` {% endraw %} ## Permalinks There are special [permalink variables for collections]({{ '/docs/permalinks/#collections' | relative_url }}) to help you control the output url for the entire collection. ## Custom Sorting of Documents {%- include docs_version_badge.html version="4.0" -%} {: #custom-sorting-of-documents} By default, two documents in a collection are sorted by their `date` attribute when both of them have the `date` key in their front matter. However, if either or both documents do not have the `date` key in their front matter, they are sorted by their respective paths. You can control this sorting via the collection's metadata. ### Sort By Front Matter Key Documents can be sorted based on a front matter key by setting a `sort_by` metadata to the front matter key string. For example, to sort a collection of tutorials based on key `lesson`, the configuration would be: ```yaml collections: tutorials: sort_by: lesson ``` The documents are arranged in the increasing order of the key's value. If a document does not have the front matter key defined then that document is placed immediately after sorted documents. When multiple documents do not have the front matter key defined, those documents are sorted by their dates or paths and then placed immediately after the sorted documents. ### Manually Ordering Documents You can also manually order the documents by setting an `order` metadata with **the filenames listed** in the desired order. For example, a collection of tutorials would be configured as: ```yaml collections: tutorials: order: - hello-world.md - introduction.md - basic-concepts.md - advanced-concepts.md ``` Any documents with filenames that do not match the list entry simply gets placed after the rearranged documents. If a document is nested under subdirectories, include them in entries as well: ```yaml collections: tutorials: order: - hello-world.md - introduction.md - concepts/basics.md - concepts/advanced.md ``` If both metadata keys have been defined properly, `order` list takes precedence. ## Liquid Attributes ### Collections Collections are also available under `site.collections`, with the metadata you specified in your `_config.yml` (if present) and the following information: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. |
In addition to any collections you create yourself, the
posts
collection is hard-coded into Jekyll. It exists whether
you have a _posts
directory or not. This is something to note
when iterating through site.collections
as you may need to
filter it out.
You may wish to use filters to find your collection:
{% raw %}{{ site.collections | where: "label", "myCollection" | first }}{% endraw %}
Except for documents in hard-coded default collection posts
, all documents in collections
you create, are accessible via Liquid irrespective of their assigned date, if any, and therefore renderable.
Documents are attempted to be written to disk only if the concerned collection
metadata has output: true
. Additionally, future-dated documents are only written if
site.future
is also true.
More fine-grained control over documents being written to disk can be exercised by setting
published: false
(true
by default) in the document's front matter.
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. |