References

Link a member to an item in another dataset by key, with the target resolved to the same instance at build time.

On this page
csharp
public sealed record BlogPost(
    string Slug, string Title,
    [Reference("Authors")] Author Author);

[Reference("Authors")] links a member to an item in another dataset. It works on Markdown, JSON and YAML datasets. The target becomes a dependency automatically, so Bevel orders the datasets for you.

markdown
---
title: Hello
authorId: ada
---

With authorId: ada, post.Author is the same Author instance that is in Data.Authors.

Options

  • From: the source key. Defaults to {member}Id, then {member}.
  • Key: the key in the target. Defaults to Id, matched as a string without regard to case. 1 matches "1".
  • Required: defaults to true. Empty keys, unknown ids and duplicate keys fail the build.

Lists (Author[], IReadOnlyList<Author>) resolve from a list of keys. The snapshot stores keys only, so each item is the same instance as the one in the target dataset.

A dataset cannot reference itself ([Reference("Posts")] on a Posts item type). Items would have to exist before they are built. The build fails with BEVEL094. Store the keys and look the items up when the page renders:

csharp
[DataSource("Posts", "Data/Posts")]
public sealed record BlogPost(
    [property: Unique] string Slug, string Title, string[]? RelatedSlugs = null);
razor
@foreach (var related in Data.Posts.Where(p => Post.RelatedSlugs?.Contains(p.Slug) == true))
{
    <a href="@Links.To(related)">@related.Title</a>
}

Good to know

  • A missing or unknown key fails the build.
  • A reference to an item that a processor removed fails the build and names the processor. See Drafts and removing items.
  • A member cannot have both [Reference] and [Process].
  • After processors run, a non-nullable reference member that is still null fails with BEVEL092.