Collections in Rift are powerful groupings of content that let you organize and access related files across your site. They're identified using the collection key in a file's front matter and loaded into context.collections during the build.
Collections are implicit. Any content file with a collection value in its front matter is added to that collection automatically. No manual registration is required.
---
title: Hello World
slug: hello
collection: posts
locale: en
tags: [intro, welcome]
---
All files with collection: posts will be available under context.collections.posts as an array of data objects.
You can define custom logic to load, filter, and transform collection content by creating a file like src/collections/posts.collection.ts.
This file should export an async collection({ locale }) function and return an array of data items.
import { getContent } from '@riftjs/collection';
export async function collection({ locale }) {
return await getContent('posts/*.{{locale}}.md', item => ({
...item.metadata,
content: item.content,
}));
}
export async function collection({ locale }) {
const res = await fetch(`https://api.example.com/posts?locale=${locale}`);
if (!res.ok) throw new Error('Failed to fetch');
return await res.json();
}
Inside your controller params() function, access any collection using context.collections.name.
export async function params({ collections, param }) {
const localized = collections.posts.filter(p => p.locale === locale);
for(const post of localized) {
param(post);
}
}
You can use collections in Nunjucks templates to loop through items and render them.
{% for post in collections.posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.content | safe }}</p>
{% endfor %}
Rift collections don't auto-group by locale. But you can do it manually in your controller or template logic.
{% for post in collections.posts[locale] %}
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
{% endfor %}
You can organize collections by any logic: locale, tags, categories, or custom keys.
{% for category in categories %}
<h3>{{ category.name }}</h3>
{% for post in category.posts %}
<h4>{{ post.title }}</h4>
{% endfor %}
{% endfor %}
Rift gives you full control over how content is grouped, localized, paginated, or transformed.