Filtering page templates

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 live site 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.

Implementing page template filters

To create a new page template filter:

  1. Open your live site project in Visual Studio.
  2. Create a new class that implements the IPageTemplateFilter interface (available in the Kentico.PageBuilder.Web.Mvc.PageTemplates namespace).
  3. Add a Filter method as prescribed by the IPageTemplateFilter interface. The method has the following input parameters:
    • IEnumerable<PageTemplateDefinition> – is a collection of all available default page templates (after applying previously registered filters). Default page templates are represented by PageTemplateDefinition objects from which you can retrieve properties of respective page templates.
    • PageTemplateFilterContext – is a context of the page for which the template is being selected. It allows you to access properties which you can use to filter page templates:
      • Culture – retrieves the culture code of the given page.
      • PageType – retrieves the page type code name for the given page.
      • ParentPage – retrieves the parent page (TreeNode object) of the given page.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 cannot 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.

Example - Landing page template filter



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" };
    }
}


Registering page template filters

Register filters by adding them to the PageBuilderFilters.PageTemplates collection on application start. For example, in the Application_Start method, 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:

Example - Registering page template filters



using Kentico.PageBuilder.Web.Mvc;

...

protected void Application_Start(IServiceCollection services)
{
    ...

    RegisterPageTemplateFilters();
}

private void RegisterPageTemplateFilters()
{
    PageBuilderFilters.PageTemplates.Add(new LandingPageTemplateFilter());
    PageBuilderFilters.PageTemplates.Add(new AnotherPageTemplateFilter());
}


Registering filters that use dependency injection

Page template filter classes support dependency injection and their constructor can have parameters (e.g., instances of services registered in the project’s DI container).

Such filters must be registered into the PageBuilderFilters.PageTemplates collection using the Add<FilterClassType> method:




using Kentico.PageBuilder.Web.Mvc;

...

protected void Application_Start(IServiceCollection services)
{
    ...

    RegisterPageTemplateFilters();
}

private void RegisterPageTemplateFilters()
{
    PageBuilderFilters.PageTemplates.Add<LandingPageTemplateFilter>();
    PageBuilderFilters.PageTemplates.Add<AnotherPageTemplateFilter>();
}


Registering page template filters

Register filters by adding them to the PageBuilderFilters.PageTemplates collection on application start. For example, in your application’s startup class (Startup.cs by default), 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:

Example - Registering page template filters



using Kentico.PageBuilder.Web.Mvc;

...

public void ConfigureServices(IServiceCollection services)
{
    ...

    RegisterPageTemplateFilters();
}

private void RegisterPageTemplateFilters()
{
    PageBuilderFilters.PageTemplates.Add(new LandingPageTemplateFilter());
    PageBuilderFilters.PageTemplates.Add(new AnotherPageTemplateFilter());
}


Registering filters that use dependency injection

Page template filter classes support dependency injection and their constructor can have parameters (e.g., instances of services registered in the project’s DI container).

Such filters must be registered into the PageBuilderFilters.PageTemplates collection using the Add<FilterClassType> method:




using Kentico.PageBuilder.Web.Mvc;

...

public void ConfigureServices(IServiceCollection services)
{
    ...

    RegisterPageTemplateFilters();
}

private void RegisterPageTemplateFilters()
{
    PageBuilderFilters.PageTemplates.Add<LandingPageTemplateFilter>();
    PageBuilderFilters.PageTemplates.Add<AnotherPageTemplateFilter>();
}