Collections
Collections help you group pages together. When a template renders, it is provided with a meta.collections
object that contains all the different pages in your site.
Collections are determinined by a template’s tags
. For every tag present in your templates, a new array is added to the collections data.
For example, a template with the following data would be added to the posts
array on the collections
object.
---
title: "My Post"
tags: ["posts"]
---
Use an index
to list all the pages in a collection.
Sorting collections
Collections can be sorted in a number of ways. You can add sort
or date
values in the template data, or simply rely on sorting templates using their file name.
The best method depends on the type of content you are working with. For example, blog posts or events make sense to sort by date
. For other parts of your site, you might want to move one or two pages around manually using the sort
option.
Some types of content, like the chapters in a book, require pages to be sorted in a very specific order. In this situation, a good solution is using numbers directly in file names to make it clear which pages come first. These numbers will be stripped using the stripSortPrefix
configuration option.
Night Owl can handle all of these use cases. The sorting algorithm runs in the following order:
- Any page with any
sort
value comes first, sorted in ascending order - Any page with any
date
value comes next, sorted in descending order (most recent first) - All other pages are sorted using the file name of the template
meta.collections.all
Every page that is rendered is also available in a special all
array in the collections object.
For example, this documentation outputs the all
collection to output.json using a template like the one below.
export default {
url: "output.json",
render: function (data) {
return JSON.stringify(data.meta.collections.all, null, 2)
},
}
Note that all pages are added to meta.collections.all
, including any templates which don’t generate HTML files. For example, the output.json
template above will be added to the all
collection.
Also, it is possible that some pages may contain data that cannot be stringified.