The content templates of your Posts are designed using Page types. Remember that this does not necessarily mean that all posts of the same Post type has to be rendered the same way, it simply means that they have the same content structure.
The preferred way of importing Post types is by using the Piranha.AttributeBuilder
package. With this package you can directly mark your Post models with the Attributes needed.
Let's first look at how the simplest possible post type could look like. This post types doesn't provide anything other than the main content are which is made up of blocks.
using Piranha.AttributeBuilder;
using Piranha.Models;
[PostType(Title = "Simple Post")]
public class SimplePost : Post<SimplePost>
{
}
To import this post type during your application startup you add the following line to your Configure
method.
using Piranha.AttributeBuilder;
var builder = new PostTypeBuilder(api)
.AddType(typeof(SimplePost));
builder.Build();
The PostTypeAttribute
has the following attributes available for configuring the behaviour of the Post Type.
[PostType(Title = "Simple Post")]
The display title to show when working with posts in the manager interface. If this property is omitted the class name of the Post Type will be used as title.
[PostType(UseBlocks = false)]
Whether or not the main block content area will be used. This can be very useful for posts displaying information that should be fixed in its formatting and you want to limit the content to the pre-defined regions.
By default, all post request are rewritten to the route /post
. Since you want to load different model types for your posts, and often render them by different views or pages you need to specify which route should handle your Post type. Let's say we have a post that also displays a hero.
[PostType(Title = "Hero Post")]
[PostTypeRoute(Title = "Default", Route = "/heropost")]
public class HeroPost : Post<HeroPost>
{
public class HeroRegion
{
[Field]
public StringField Title { get; set; }
[Field]
public ImageField Image { get; set; }
[Field]
public TextField Body { get; set; }
}
[Region]
public HeroRegion Hero { get; set; }
}
By adding the PostTypeRouteAttribute
to your post type, all requests for post of this page type will now be routed to /heropost
.
Let's say we would also like use render Hero Post as an extra wide campaign post. We can achieve this by add a second PostTypeRouteAttribute
to the class.
[PostType(Title = "Hero Post")]
[PostTypeRoute(Title = "Default", Route = "/heropost")]
[PostTypeRoute(Title = "Super wide", Route = "/superwide")]
public class HeroPost : Post<HeroPost>
{
...
}
By adding a second route the post settings in the manager will now show a dropdown where the editor can select which route the current post should use.