Search code examples
c#.net-6.0piranha-cms

Piranha CMS: base class for content types


I have several types of pages. But they have the same regions with settings. The question is as follows. Is it possible to define a base class with common regions for inheritance organization? So as not to define the same thing in different classes manually.

Has no one ever wondered? Suppose I have 4 content types of pages. But they all have, say, one StringField for some purpose. Now one have to declare this field in every page class, instead of making a base class for all these types. Piranha uses an intermediate GenericPage class to organize inheritance. I don't understand how to implement a base class for content types. Or I don't know something.


Solution

  • You can just use normal c# inheritance.

    My base class

    public class PageBase : Page<PageBase>
    {
        public class CoverRegion
        {
            [Field]
            public StringField CoverText { get; set; }
            [Field]
            public ImageField CoverImg { get; set; }
        }
        
        [Region]
        public CoverRegion Cover { get; set; }
    
    }
    

    Then the class that inherits

    [PageType(Title = "Inherited Content")]
    [ContentTypeRoute(Title = "Default", Route = "page-content")]
    public class PageContent : PageBase
    {
        [Region]
        public StringField Header { get; set; }
    }
    

    Remember a region can not only contain one property, must have more. If there is only one property then set the region attribute on the property instead of field