XperienceCommunity.PageTemplateUtilities 1.0.0

A set of utilities to help filter and register Page Templates in a Kentico Xperience 13.0+ application

Install-Package XperienceCommunity.PageTemplateUtilities -Version 1.0.0
dotnet add package XperienceCommunity.PageTemplateUtilities --version 1.0.0
<PackageReference Include="XperienceCommunity.PageTemplateUtilities" Version="1.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add XperienceCommunity.PageTemplateUtilities --version 1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: XperienceCommunity.PageTemplateUtilities, 1.0.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install XperienceCommunity.PageTemplateUtilities as a Cake Addin
#addin nuget:?package=XperienceCommunity.PageTemplateUtilities&version=1.0.0

// Install XperienceCommunity.PageTemplateUtilities as a Cake Tool
#tool nuget:?package=XperienceCommunity.PageTemplateUtilities&version=1.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

Xperience Page Template Utilities

NuGet Package

Utilities to help quickly create and register MVC Page Templates in Kentico Xperience

Dependencies

This package is compatible with ASP.NET Core 3.1 → ASP.NET Core 5 and is designed to be used with .NET Core / .NET 5 Console applications integrated with Kentico Xperience 13.0.

How to Use?

  1. First, install the NuGet package in your ASP.NET Core project

    dotnet add package XperienceCommunity.PageTemplateUtilities
    
  2. Create an implementation of PageTypePageTemplateFilter for a given Page Type and register some Page Templates

    [assembly: RegisterPageTemplate(
        "Sandbox.HomePage_Default",
        "Home Page (Default)",
        typeof(BasicContentPageTemplateProperties),
        "~/Features/Home/Sandbox.HomePage_Default.cshtml")]
    
    public class HomePageTemplateFilter : PageTypePageTemplateFilter
    {
         public override string PageTypeClassName => "Sandbox.HomePage";
    }
    
  3. Register all IPageTemplateFilter implementations with the IServiceCollection

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddPageTemplateFilters(Assembly.GetExecutingAssembly());
        }
    }
    

How Does It Work?

The PageTypePageTemplateFilter is simplest if your Page Templates follow some conventions, but allows for full customization.

In the example below, Home Page (Default) will match the filter and be allowed for selection for Sandbox.HomePage
Page Types, but Home Page (Featured) does not have an Identifier (Sandbox_PageTemplate_HomePage_Featured)
that matches the prefix of Sandbox.HomePage_, so it will not be displayed for selection for
Sandbox.HomePage Page Types. It would need to be changed to Sandbox.HomePage_Featured to match the default filter, which follows the pattern of matching Page Templates with a Identifier prefix of $"{PageTypeClassName}_".

[assembly: RegisterPageTemplate(
    "Sandbox.HomePage_Default",
    "Home Page (Default)",
    typeof(BasicContentPageTemplateProperties),
    "~/Features/Home/Sandbox.HomePage_Default.cshtml")]

[assembly: RegisterPageTemplate(
    "Sandbox_PageTemplate_HomePage_Featured",
    "Home Page (Featured)",
    typeof(BasicContentPageTemplateProperties),
    "~/Features/Home/Sandbox_HomePage_Featured.cshtml")]

public class HomePageTemplateFilter : PageTypePageTemplateFilter
{
    public override string PageTypeClassName => HomePage.CLASS_NAME;
}

The filter can be overridden to allow you to match however you would like. It has a signature of:

Func<PageTemplateDefinition, PageTemplateFilterContext, string, bool> PageTemplateFilterBy

The customization below would match an Identifier like _Sandbox.HomePage-Default:

public class HomePageTemplateFilter : PageTypePageTemplateFilter
{
    public override string PageTypeClassName => HomePage.CLASS_NAME;

    public override Func<PageTemplateDefinition, PageTemplateFilterContext, string, bool> PageTemplateFilterBy { get; } =
        (definition, context, className) => definition.Identifier.StartsWith($"_{className}-", StringComparison.OrdinalIgnoreCase);
}

We could even skip using the className entirely and match against the definition or context with some hardcoded values,
but at that point it's probably best to implement the IPageTemplateFilter directly.

References

Kentico Xperience

Xperience Page Template Utilities

NuGet Package

Utilities to help quickly create and register MVC Page Templates in Kentico Xperience

Dependencies

This package is compatible with ASP.NET Core 3.1 → ASP.NET Core 5 and is designed to be used with .NET Core / .NET 5 Console applications integrated with Kentico Xperience 13.0.

How to Use?

  1. First, install the NuGet package in your ASP.NET Core project

    dotnet add package XperienceCommunity.PageTemplateUtilities
    
  2. Create an implementation of PageTypePageTemplateFilter for a given Page Type and register some Page Templates

    [assembly: RegisterPageTemplate(
        "Sandbox.HomePage_Default",
        "Home Page (Default)",
        typeof(BasicContentPageTemplateProperties),
        "~/Features/Home/Sandbox.HomePage_Default.cshtml")]
    
    public class HomePageTemplateFilter : PageTypePageTemplateFilter
    {
         public override string PageTypeClassName => "Sandbox.HomePage";
    }
    
  3. Register all IPageTemplateFilter implementations with the IServiceCollection

    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddPageTemplateFilters(Assembly.GetExecutingAssembly());
        }
    }
    

How Does It Work?

The PageTypePageTemplateFilter is simplest if your Page Templates follow some conventions, but allows for full customization.

In the example below, Home Page (Default) will match the filter and be allowed for selection for Sandbox.HomePage
Page Types, but Home Page (Featured) does not have an Identifier (Sandbox_PageTemplate_HomePage_Featured)
that matches the prefix of Sandbox.HomePage_, so it will not be displayed for selection for
Sandbox.HomePage Page Types. It would need to be changed to Sandbox.HomePage_Featured to match the default filter, which follows the pattern of matching Page Templates with a Identifier prefix of $"{PageTypeClassName}_".

[assembly: RegisterPageTemplate(
    "Sandbox.HomePage_Default",
    "Home Page (Default)",
    typeof(BasicContentPageTemplateProperties),
    "~/Features/Home/Sandbox.HomePage_Default.cshtml")]

[assembly: RegisterPageTemplate(
    "Sandbox_PageTemplate_HomePage_Featured",
    "Home Page (Featured)",
    typeof(BasicContentPageTemplateProperties),
    "~/Features/Home/Sandbox_HomePage_Featured.cshtml")]

public class HomePageTemplateFilter : PageTypePageTemplateFilter
{
    public override string PageTypeClassName => HomePage.CLASS_NAME;
}

The filter can be overridden to allow you to match however you would like. It has a signature of:

Func<PageTemplateDefinition, PageTemplateFilterContext, string, bool> PageTemplateFilterBy

The customization below would match an Identifier like _Sandbox.HomePage-Default:

public class HomePageTemplateFilter : PageTypePageTemplateFilter
{
    public override string PageTypeClassName => HomePage.CLASS_NAME;

    public override Func<PageTemplateDefinition, PageTemplateFilterContext, string, bool> PageTemplateFilterBy { get; } =
        (definition, context, className) => definition.Identifier.StartsWith($"_{className}-", StringComparison.OrdinalIgnoreCase);
}

We could even skip using the className entirely and match against the definition or context with some hardcoded values,
but at that point it's probably best to implement the IPageTemplateFilter directly.

References

Kentico Xperience

Release Notes

https://github.com/wiredviews/xperience-page-template-utilities/releases

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
1.0.0 210 3/20/2021
1.0.0-alpha-1 134 3/20/2021