Rift Logo RiftJS

Collections

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.

1. Declaring Collections in Content

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.

Example: Markdown Frontmatter
---
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.

2. Creating a Collection Loader

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.

Example: File-based Loader
import { getContent } from '@riftjs/collection';

export async function collection({ locale }) {
  return await getContent('posts/*.{{locale}}.md', item => ({
    ...item.metadata,
    content: item.content,
  }));
}
Example: Remote API Loader
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();
}

3. Using Collections in Controllers

Inside your controller params() function, access any collection using context.collections.name.

Controller Example
export async function params({ collections, param }) {
    const localized = collections.posts.filter(p => p.locale === locale);
    for(const post of localized) {
        param(post);
    }
}

4. Using Collections in Templates

You can use collections in Nunjucks templates to loop through items and render them.

Template Loop
{% for post in collections.posts %}
  <h2>{{ post.title }}</h2>
  <p>{{ post.content | safe }}</p>
{% endfor %}

5. Localization

Rift collections don't auto-group by locale. But you can do it manually in your controller or template logic.

Looping Localized Content
{% for post in collections.posts[locale] %}
  <h2>{{ post.title }}</h2>
  <p>{{ post.content }}</p>
{% endfor %}

6. Custom Grouping

You can organize collections by any logic: locale, tags, categories, or custom keys.

Example: Group by Category
{% 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.