When users create a new page or change the template of an existing page, the system displays a list of available page templates. By default, the list contains all of the page templates registered in the current MVC project.
Implement page template filtering to control what page templates are available to users. This is necessary to ensure that the page template selection dialog only appears for pages that use and support templates. You can also use filtering to display only templates that are relevant for specific pages. For example, only display landing page templates when creating landing pages.
When filters are implemented and registered, the page template selection dialog only shows default templates that are allowed for the current page. Additionally, it also shows all custom templates that are based on the allowed default templates.
To create a new page template filter:
Open your MVC project in Visual Studio.
Create a new class that implements the IPageTemplateFilter interface (available in the namespace).
Add a Filter method as prescribed by the IPageTemplateFilter interface. The method has the following input parameters:
The Filter method needs to return a collection of page templates, which is then passed to other registered filters and eventually to the template selection dialog. You can not return NULL or an empty collection from a filter, as this would invalidate all subsequent filters and no templates will be displayed in the template selection dialog.
It is recommended to divide page templates into mutually exclusive categories by their intended use, for example home page templates, article page templates, and landing page templates. You can then create filters, which, according to the context of the current page, either return page templates of the suitable category or all other page templates.
using System;
using System.Collections.Generic;
using System.Linq;
using Kentico.PageBuilder.Web.Mvc.PageTemplates;
namespace LearningKit.PageTemplateFilters
{
public class LandingPageTemplateFilter : IPageTemplateFilter
{
public IEnumerable<PageTemplateDefinition> Filter(IEnumerable<PageTemplateDefinition> pageTemplates, PageTemplateFilterContext context)
{
// Applies filtering to a collection of page templates based on the page type of the currently edited page
if (context.PageType.Equals("LearningKit.LandingPage", StringComparison.InvariantCultureIgnoreCase))
{
// Filters the collection to only contain filters allowed for landing pages
return pageTemplates.Where(t => GetLandingPageTemplates().Contains(t.Identifier));
}
// Excludes all landing page templates from the collection if the context does not match this filter
// Assumes that the categories of page templates are mutually exclusive
return pageTemplates.Where(t => !GetLandingPageTemplates().Contains(t.Identifier));
}
// Gets all page templates that are allowed for landing pages
public IEnumerable<string> GetLandingPageTemplates() => new string[] { "LearningKit.LandingPageTemplate" };
}
}Register filters by adding them to the PageBuilderFilters.PageTemplates collection on application start. For example, in the Application_Start method in Global.asax.cs, or as part of custom module initialization code.
Note that the filtering is cumulative. The process starts with a collection of all page templates registered in the system. Each subsequently applied filter then works with the collection of templates returned (filtered) by the previous filter:
using Kentico.PageBuilder.Web.Mvc;
...
protected void Application_Start()
{
...
RegisterPageTemplateFilters();
}
private void RegisterPageTemplateFilters()
{
PageBuilderFilters.PageTemplates.Add(new LandingPageTemplateFilter());
PageBuilderFilters.PageTemplates.Add(new AnotherPageTemplateFilter());
}