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" />
paket add XperienceCommunity.PageTemplateUtilities --version 1.0.0
#r "nuget: XperienceCommunity.PageTemplateUtilities, 1.0.0"
// 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
Xperience Page Template Utilities
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?
First, install the NuGet package in your ASP.NET Core project
dotnet add package XperienceCommunity.PageTemplateUtilities
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"; }
Register all
IPageTemplateFilter
implementations with theIServiceCollection
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
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?
First, install the NuGet package in your ASP.NET Core project
dotnet add package XperienceCommunity.PageTemplateUtilities
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"; }
Register all
IPageTemplateFilter
implementations with theIServiceCollection
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
Dependencies
-
.NETCoreApp 3.1
- Kentico.Xperience.AspNetCore.WebApp (>= 13.0.0 && < 13.1.0)
- Scrutor (>= 3.3.0)
Used By
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 |