Contribute to Docs Contribute to Docs

Archives

Before reading about Archives your should first read Pages so that you have a basic understanding of how to create Page Types for regular and archive pages.

Archive pages has the same content features as a regular page, but they also hold a collection of unique Post, Categories and Tags.

Your First Archive Type

As an example, let's take a look at how the simplest possible archive would look like. This archive will only contain posts and will not have any content of its own, not even blocks.

using Piranha.AttributeBuilder;
using Piranha.Models;

[PageType(Title = "Simple Archive", UseBlocks = false, IsArchive = true)]
public class SimpleArchive : Page<SimpleArchive>
{
}

Adding an Archive Property

When rendering your archive you'll most likely want a property containing the current state, and the available posts of the archive. We provide the class PostArchive<T> for this, which is also the type returned by the ArchiveService. When adding your Archive property you should first consider what you'll use it from and what kind of posts will be a part of the archive.

Single Type

If you have restricted your archive to contain a single Post Type the most convenient approach is to get all posts as the specified type. Please note, that if the archive contains posts of other Post Types these will be excluded.

[PageType(Title = "Simple Archive", UseBlocks = false, IsArchive = true)]
public class SimpleArchive : Page<SimpleArchive>
{
    public PostArchive<SimplePost> Archive { get; set; }
}

var model = await api.Pages.GetByIdAsync<SimpleArchive>(id);
model.Archive = await api.Archives.GetByIdAsync<SimplePost>(...);

Base Class

If you have multiple allowed Post Types you can define your archive as the type PostBase which is the base class of all posts. Remember that when getting the archive this way you have to check what type each post is or use separate DisplayTemplates for them.

[PageType(Title = "Simple Archive", UseBlocks = false, IsArchive = true)]
public class SimpleArchive : Page<SimpleArchive>
{
    public PostArchive<PostBase> Archive { get; set; }
}

var model = await api.Pages.GetByIdAsync<SimpleArchive>(id);
model.Archive = await api.Archives.GetByIdAsync<PostBase>(...);

foreach (var post in model.Archive)
{
    if (post is BlogPost)
    {
        ...
    }
    else if (post is NewsPost)
    {
        ...
    }
}

Info Class

If you don't need any regions or blocks in your listing, your can request the archive using the PostInfo class. These models are also cached.

[PageType(Title = "Simple Archive", UseBlocks = false, IsArchive = true)]
public class SimpleArchive : Page<SimpleArchive>
{
    public PostArchive<PostInfo> Archive { get; set; }
}

var model = await api.Pages.GetByIdAsync<SimpleArchive>(id);
model.Archive = await api.Archives.GetByIdAsync<PostInfo>(...);