Developing page templates in MVC
Page templates allow content editors to gain greater control over the layout of pages, without the need to ask a developer to modify the code in the MVC project.
Templates make it easy to choose and switch between page layouts, so they are suitable for creating pages with a predesigned or repeating structure, such as landing pages. Developers define the layout of these pages by preparing page templates in the code and content editors then create new landing pages based on these templates.
Content editors can also utilize page templates for creating pages with structured content (even without any page builder editable areas). This can be useful, for example if you have multiple possible layouts for product or article pages, and wish to allow editors to choose a suitable layout for each individual page.
When creating a page , content editor can choose from two types of page templates:
- Default templates are defined in the code of the MVC project. They specify the layout of a page, and the content can be then supplied by a content editor.
- Custom templates are based on default templates. On top of the layout defined in the original default template, custom templates also contain a snapshot of page builder content, such as widgets and sections, taken from an existing page. Structured data stored in the fields of pages is not included within custom templates. See how to create custom page templates.
Page template filtering
When you register page templates in your solution, you need to implement page template filtering to limit page templates only for specific page types. Otherwise, an error occurs when creating pages of any page type with the Page builder feature enabled, but no available page template.
Creating pages that support page templates
To be able to utilize the benefits of page templates on your pages, you first need to create and configure content-only page types and adjust the controllers that handle displaying of the given pages.
Start by preparing a content-only page type for your template-based pages:
- In the Page types application, create a content-only page type.
- Edit () the created page type, and perform the following on the General tab:
- Specify the URL pattern for the page type.
- Enable the Use Page tab setting.
- Click Save.
In your MVC site’s code, create a controller with a GET action that handles rendering of the pages:
The GET action must return a TemplateResult object.
You need to set the pageIdparameter of the TemplateResult constructor to the ID identifier of the rendered page. You can get the ID value via the DocumentID property of the TreeNode object that represents the page.
Example/// <summary> /// A GET action displaying the page where you wish to use page templates. /// </summary> /// <param name="pageAlias">Page alias of the displayed page.</param> public ActionResult Index(string pageAlias) { // Retrieves the page from the Kentico database TreeNode page = DocumentHelper.GetDocuments() .Path("/Landing-pages", PathTypeEnum.Children) .WhereEquals("NodeAlias", pageAlias) .OnCurrentSite() .TopN(1) .FirstOrDefault(); // Returns a 404 error when the retrieving is unsuccessful if (page == null) { return HttpNotFound(); } // Returns a TemplateResult object, created with an identifier of the page as its parameter // Automatically initializes the page builder feature for any editable areas placed within templates return new TemplateResult(page.DocumentID); }
Notes
- When developing pages that use page templates, you do NOT need to create a view for the page itself. Instead, the output of the page is based on the view of the selected template.
- Returning the TemplateResult object automatically performs the API initialization of the page builder feature for any editable areas placed within the templates.
When creating new pages of the given type in the Pages application, content editors can now select a page template. Registered page templates are automatically available for selection, depending on predefined page template filters. If only one template is available for a given page, it is selected automatically.
The following steps describe how Kentico MVC applications handle requests for pages that use a page template:
- The request goes through the standard routing process and is handled by a specific controller action (needs to be implemented).
- The action returns a TemplateResult, initialized with the ID of the given Kentico page.
- Based on the ID, the system gets the template (along with any configured properties) and page builder content for the specified page.
- The template’s controller (either default or custom) returns and displays the page content based on the template’s view (see Implementing page templates below for details).
Implementing page templates
On a basic technical level, page templates are HTML pages. The main step in the development of a page template is to create a full page view that defines the output.
Within the MVC architecture, the page template view is served by a controller and a model is used to pass any required data. In many cases, templates can utilize a default controller and view model provided by the Kentico API. See the following scenarios for more information:
In both cases you can develop page templates with properties, which allow content editors to customize the template appearance in the Kentico administration interface. For templates with configurable properties, you need to create an additional model class that represents the properties and passes their values to the controller. See Defining page template properties to learn more.
MVC Areas
Page templates are designed to be used in the global scope and their code files must be placed in the application root of your MVC project (not in an MVC Area). Creating page templates in MVC Areas may lead to unexpected behavior.
Basic page templates
Use the following process to develop a page template:
- Create a view with code that defines the output of the page template according to the general MVC best practices.
The output must be a full HTML page, so the view must include the following:
- Full HTML markup, including the html, head and body elements
- Links to all necessary resources, such as stylesheets and scripts
- Links to page builder scripts and styles
Use MVC layouts with the template view for any shared output code (based on your requirements, you can use your site’s main layout, a dedicated layout for page templates, etc.).
We recommend storing page template views in the ~/Views/Shared/PageTemplates folder, and using a view name that matches the identifier assigned to the template upon its registration prefixed with the underscore (‘_’) character. Alternatively, you can use any required view location or name, and then specify it when registering the template.
Accessing the template’s page
If you need to work with the data of the page using the currently processed page template, use the ComponentViewModel class as the view’s model and access its Page property. The property returns a TreeNode object representing the given page. If you need to load values from the fields of a specific page type, you can convert the TreeNode object to an instance of a specific page type wrapper class (the page using the template must then be of the given page type).
- Register the page template into the system. See Registering page templates.
With this approach, the template’s view is automatically displayed using a default controller provided by the Kentico API. The values of any properties defined for the template can be passed to the view by using the default ComponentViewModel<TPropertyModel> class as the model.
Example of page template development
To see a scenario with full code samples which will guide you through the process of developing a simple template, visit Example - Developing a page template with a configurable property.
Page templates with a custom controller
When developing page templates with advanced functionality, you may need to take full control over the template’s logic. You can do this by implementing the template’s controller and view model, in addition to the view. This allows you to run any custom code within the template’s controller, pass any type of required data to the view, or even switch between completely different views based on the current scenario.
The following steps describe the advanced development process for page templates:
Create a controller class for the page template.
- We recommend storing template controllers in the ~/Controllers/PageTemplates folder.
Make the controller inherit from the PageTemplateController base class (available in the Kentico.PageBuilder.Web.Mvc.PageTemplates namespace).
Implement the default Index action in the controller, which is used to retrieve the template markup. The action must return the page template’s HTML content, typically a view.
Accessing the template’s page
If you need to access fields of the page using the currently processed page template, call the generic GetPage<TPageType> method (provided by the PageTemplateController base class). The method takes a page type wrapper class as the PageType parameter and returns a page object of the specified page type. Alternatively, you can call the GetPage method returning only a TreeNode object representing the given page.
// Gets the page of the Article page type containing the currently processed page template var article = GetPage<Article>();
Notes
- Do not disable POST requests for the Index action (e.g., by using the HttpGet attribute). POST requests to the Index action are used in the page builder feature.
- Template controller actions used to retrieve the markup cannot be asynchronous (cannot use the async function declaration). Actions that render template markup are called as child actions when rendering the markup of a page, but MVC 5 does not support asynchronous child controller actions.