Contribute to Docs Contribute to Docs

Posts

The content templates of your Posts are designed using Post 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.

Your First Post Type

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();

You can also define a single import for all of the content types in your application by adding the assemblies containing your types. The benefit of this is that you don't have to update your Startup code when adding new types.

using Piranha.AttributeBuilder;

var builder = new ContentTypeBuilder(api)
    .AddAssembly(typeof(Startup));
builder.Build();

Post Type Configuration

The PostTypeAttribute has the following attributes available for configuring the behaviour of the Post Type.

Title
[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.

UseBlocks
[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.

UseExcerpt
[PostType(UseExcerpt = false)]

If the excerpt should be available when working with posts in the manager interface. The default setting is true.

UsePrimaryImage
[PostType(UsePrimaryImage = false)]

If the primary image should be available when working with posts in the manager interface. The default setting is true.

Post Routing

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.

using Piranha.AttributeBuilder;
using Piranha.Extend;
using Piranha.Extend.Fields;
using Piranha.Models;

[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.

Multiple Post Routes

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.

Securing Posts

You can secure posts by adding permissions to them that the current user must have in order to have access to them. By adding one or more permission to a post also means that the user has to authenticated in order to access the post. This can be done both from the manager interface but also from code.

myPost.Permissions.Add("WebUser");

You can read more about how to add custom permissions to your application in Authentication.

Advanced Content

Now that you know the basics for setting up posts, you should continue to read about the different components available for creating more advanced content. Information about this can be found in the articles Blocks, Regions and Fields.