Night Owl logoNight Owl

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:

  1. Any page with any sort value comes first, sorted in ascending order
  2. Any page with any date value comes next, sorted in descending order (most recent first)
  3. 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.

Page data

This page was rendered with the following data.

{
  "layout": "_/layouts/docs",
  "menu": {...},
  "filters": {...},
  "helpers": {...},
  "version": "0.5.4",
  "github": "https://github.com/stephenhutchings/night-owl/",
  "npm": "https://www.npmjs.com/package/night-owl",
  "hash": "lmu1z5z4",
  "thumbnail": "https://night-owl.s-ings.com/assets/thumb.png",
  "isDev": false,
  "tags": [ "templates" ],
  "title": "Collections",
  "description": "Group pages together using tags",
  "url": "/docs/templates/collections/",
  "meta": {
    "url": "/docs/templates/collections/",
    "src": "src/docs/04-templates/04-collections.md",
    "dist": "dist/docs/templates/collections/index.html",
    "collections": {
      "all": [...],
      "intro": [...],
      "commands": [...],
      "configuration": [...],
      "advanced": [...],
      "templates": [...]
    }
  },
  "content": "<p>Collections help you group pages together. When a template renders, it is\npro..."
}