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)]
public class SimpleArchive : ArchivePages<SimpleArchive>
{
}
To import this page type during your application startup you add the following line to your Configure
method.
using Piranha.AttributeBuilder;
var builder = new PageTypeBuilder(api)
.AddType(typeof(SimpleArchive));
builder.Build();
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)]
[PageTypeArchiveItem(typeof(SimplePost))]
public class SimpleArchive : ArchivePage<SimpleArchive>
{
}
By default the PostArchive
Property of the Page model contains posts of the type DynamicPost
. The downside of this is that dynamic models are not cached, which will result in a higher load on the database. You can create a generic Archive Page to allow for greater flexibility.
using Piranha.AttributeBuilder;
using Piranha.Models;
[PageType(Title = "Simple Archive", UseBlocks = false)]
public class SimpleArchive<T> : ArchivePage<Simple, T> where T : PostBase
{
}
Below are a few examples on how this can be used.
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.
var model = await api.Pages.GetByIdAsync<SimpleArchive<BlogPost>>(id);
model.Archive = await api.Archives.GetByIdAsync<BlogPost>(...);
If you have multiple allowed Post Types, but want to get all posts as their type you can request them using the PostBase
class. Remember that you have to check what type each post is or use separate DisplayTemplates for them.
var model = await api.Pages.GetByIdAsync<SimpleArchive<PostBase>>(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.
var model = await api.Pages.GetByIdAsync<SimpleArchive<PostInfo>>(id);
model.Archive = await api.Archives.GetByIdAsync<PostInfo>(...);
All archive pages have a custom editor registered by default that shows the post archive for the user. The default editor is added with the title Archive which means it can be replaced by adding a new editor with the same name. You can also add other custom editors to your archive page, just like with a regular page.
[PageType(Title = "Simple Archive")]
[PageTypeEditor(Title = "Archive", Component = "custom-archive", Icon = "fas fa-fish")]
public class SimpleArchive : ArchivePage<SimpleArchive>
{
...
}
Custom editors are implemented as global Vue components
and are responsible for handling their own data, both loading and saving. Custom components are added to the manager by registering custom javscript resource in the manager module. You can read more about this in Resources in the section Manager Extensions.