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.
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>
{
}
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.
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>(...);
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)
{
...
}
}
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>(...);
By default any type of posts can be added to an Archive Page, but you can specify this by adding one or more PageTypeArchiveItemAttribute
to your type.
using Piranha.AttributeBuilder;
using Piranha.Models;
[PageType(Title = "Simple Archive", UseBlocks = false, IsArchive = true)]
[PageTypeArchiveItem(typeof(SimplePost))]
public class SimpleArchive : Page<SimpleArchive>
{
public PostArchive<SimplePost> Archive { get; set; }
}