Data Collections
Data collections are groups of data on a website.
You can use collections to create lists of content items (i.e. all of the bookmarks on your website).
You can create a data collection by:
- Loading data from a JSON file
- Loading data from a CSV file
- Specifying a
collections
value on any page on your website
Create a Collection
JSON
To create a collection from a JSON file, add a new file to your site's
pages/_data
directory. This file should have a
.json
extension.
Within the file, create a list that contains JSON objects, like this:
[
{
"slug": "rosslyn-coffee",
"layout": "coffee",
"title": "Rosslyn Coffee in London is terrific."
}
]
This file is called
pages/_data/coffee.json
.
Every entry must have a layout
key. This corresponds
with the name of the template that will be used to render the page. For
example, the coffee
layout will be rendered using the
pages/_layouts/coffee.html
template.
Every entry must also have a slug
key. This corresponds
with the name of the page that will be generated. In the case above, one
file will be created in the _site
output directory:
_site/coffee/rosslyn-coffee/index.html
.
CSV
To create a collection from a CSV file, add a new file to your site's
pages/_data
directory. This file should have a
.csv
extension.
Here is an example CSV file:
slug,layout,title
rosslyn-coffee,coffee,Rosslyn Coffee in London is terrific.
Your CSV file must have a header row that contains the keys for each entry.
This file is called
pages/_data/coffee.csv
.
Every entry must have a layout
key. This corresponds
with the name of the template that will be used to render the page. For
example, the coffee
layout will be rendered using the
pages/_layouts/coffee.html
template.
Every entry must also have a slug
key. This corresponds
with the name of the page that will be generated. In the case above, one
file will be created in the _site
output directory:
_site/coffee/rosslyn-coffee/index.html
.
Specify a Collections Attribute
If you want to group multiple existing files together, you can specify a collections
attribute on any page on your website.
To do so, use the following syntax:
---
title: My Page
collections: coffee
---
You can then access the collection like so:
{% raw %}{% for item in coffee %}{% endraw %}
{{ item.title }}
{% raw %}{% endfor %}{% endraw %}