# Create custom templates based on layout files

## Overview

Every ASP.NET Core page is based on a layout file in the Renderer project. The layout file holds the common HTML structure of the page, such as the `<head>` and `<body>` tags, and the common styles and scripts. It also defines the placeholders where editors add widgets in the New page editor.

Out-of-the-box, the ASP.NET Core Renderer comes with a blank layout file that is displayed as the *Default* template. In most cases, you create custom templates in the backend, based on the *Default* template. For more information, see [Create custom templates in the backend](https://www.progress.com/documentation/sitefinity-cms/create-custom-templates-in-the-backend).

When you need your own markup, styles, scripts, or placeholders, create a custom layout file. A custom layout file is an ASP.NET Core [layout file](https://learn.microsoft.com/en-us/aspnet/core/mvc/views/layout) that you place in the Renderer project. The Renderer scans the layout files in the `Views`/`Shared` folder and displays them in the template selector when you create a page or a page template.

> info **PREREQUISITES**: You have set up the ASP.NET Core Renderer. For more information, see [Project template](https://www.progress.com/documentation/sitefinity-cms/project-template).

## Layout file requirements

Every layout file that you use as a template must meet the following requirements:

* The file is located in the `Views`/`Shared` folder of the Renderer project.
* The file declares the page model:\
  `@model Progress.Sitefinity.AspNetCore.Models.PageModel`
* The file references the tag helpers from the `Progress.Sitefinity.AspNetCore` namespace:\
  `@addTagHelper *, Progress.Sitefinity.AspNetCore`
* The file renders a section named `Scripts`:\
  `@RenderSection("Scripts")`

> info **NOTE**: In Sitefinity CMS versions earlier than 14.3.8023, the name of the layout file must contain the word *Layout* or *Template*. Otherwise, the file is not displayed in the template selector. As of version 14.3.8023, this naming requirement no longer applies.

## Create the layout file

To create a custom layout file, perform the following:

1. In your Renderer project, open the `Views`/`Shared` folder.

2. Create a new file and name it, for example, `SitefinityTemplate.cshtml`.\
   The file name, without the extension, is the name of the template in the template selector.

3. Paste the following code in the file:

   ```HTML
   @model Progress.Sitefinity.AspNetCore.Models.PageModel
   @using Progress.Sitefinity.AspNetCore.Mvc.Rendering

   @addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
   @addTagHelper *, Progress.Sitefinity.AspNetCore

   @inject Progress.Sitefinity.AspNetCore.Web.IRequestContext requestContext;
   @inject Progress.Sitefinity.AspNetCore.Web.IRenderContext renderContext;

   <!DOCTYPE html>
   <html id="html" lang="@requestContext.Culture">
   <head>
       <meta charset="utf-8" />
       <meta name="viewport" content="width=device-width, initial-scale=1.0" />
       <link rel="icon" type="image/x-icon" href="/favicon.ico">

       @Html.RenderSeoMeta(this.Model)

       @* Styles for the out-of-the-box Sitefinity widgets *@
       <link rel="stylesheet" href="@Progress.Sitefinity.AspNetCore.Constants.PrefixRendererUrl("Styles/bootstrap.min.css")" />
       <link rel="stylesheet" href="@Progress.Sitefinity.AspNetCore.Constants.PrefixRendererUrl("styles/main.css")" type="text/css" />

       @* Custom styles *@
       <link rel="stylesheet" href="~/css/site.css" />

       @if (this.renderContext.IsEdit)
       {
           @* Custom styles loaded only in the page editor *@
           <link rel="stylesheet" href="~/css/site-edit.css" />
       }
   </head>

   <body class="container-fluid">
       <header data-sfcontainer="Header">
       </header>
       <main data-sfcontainer="Content">
       </main>
       <footer data-sfcontainer="Footer">
       </footer>

       @* Custom scripts *@
       <script src="~/js/site.js"></script>

       @RenderSection("Scripts")
   </body>
   </html>
   ```

4. Build and run the Renderer project.

The code above does the following:

* `@Html.RenderSeoMeta(this.Model)` renders the SEO and Open Graph metadata of the page in the `<head>` tag. For more information, see [Advanced widget settings](https://www.progress.com/documentation/sitefinity-cms/advanced-wdget-settings).
* `IRequestContext` provides the current language, which the template uses for the `lang` attribute of the `<html>` tag.
* `IRenderContext` provides the `IsEdit` property, which the template uses to load styles only when the page is open in the page editor. For more information, see [Access context data](https://www.progress.com/documentation/sitefinity-cms/access-context-data).
* The `<header>`, `<main>`, and `<footer>` elements are the placeholders where editors add widgets.
* `@RenderSection("Scripts")` renders the scripts that the widgets on the page require.

## Define placeholders for widgets

The placeholders define where editors can drop widgets on pages based on your template. To define a placeholder, decorate an HTML element with the `data-sfcontainer` attribute. The value of the attribute is the name of the placeholder that appears in the page editor.

You can use the `data-sfcontainer` attribute on the following HTML elements:

* `<body>`
* `<header>`
* `<main>`
* `<footer>`
* `<section>`
* `<aside>`
* `<div>`

For example, the following markup defines a sidebar and a main content area:

```HTML
<body class="container">
    <div class="row">
        <aside class="col-md-3" data-sfcontainer="Sidebar">
        </aside>
        <main class="col-md-9" data-sfcontainer="Content">
        </main>
    </div>
    @RenderSection("Scripts")
</body>
```

To create the columns and rows inside each placeholder, editors use the *Section* widget. For more information, see [Design the layout with the Section widget](https://www.progress.com/documentation/sitefinity-cms/style-the-section-widget).

## Add styles and scripts

To use your own CSS and JavaScript files in the template, perform the following:

1. Make sure the Renderer serves static files.\
   In the `Program.cs` file, verify that the following line is present before `app.UseSitefinity();`:\
   `app.UseStaticFiles();`
2. Place your styles and scripts in the `wwwroot` folder of the Renderer project, for example, in `wwwroot/css` and `wwwroot/js`.
3. Reference the files in the layout file, using the `~/` prefix, for example:\
   `<link rel="stylesheet" href="~/css/site.css" />`

The out-of-the-box widgets of the ASP.NET Core Renderer use the styles in the `styles/main.css` file that comes with the Renderer. To reference this file, use the `Progress.Sitefinity.AspNetCore.Constants.PrefixRendererUrl` method, as shown in the sample layout file above. The *Default* template and the default *Section* widget templates use the Bootstrap 5 grid system. If you build your template with a different CSS framework, you must also customize the *Section* widget templates. For more information, see [Style the widgets and pages](https://www.progress.com/documentation/sitefinity-cms/style-.net-core-widgets-and-pages).

You can also load a script or a style only on specific pages. For example, the following code loads `site.js` only on a page with the title *my_page_with_script*:

```HTML
@if (requestContext.PageNode.Title == "my_page_with_script")
{
    <script src="~/js/site.js" asp-append-version="true"></script>
}
```

## Use the layout file

After you build and run the Renderer project, your layout file is displayed in the template selector.

### Create a page based on the layout file

1. In Sitefinity CMS backend, navigate to *Pages* and click *Create a page*.
2. Enter the title of the page and click *Continue*.
3. On the *Select a template* step, scroll to the *.NET Core templates* section and select your template, for example, *SitefinityTemplate*.\
   The page opens in the New page editor. The placeholders that you defined with the `data-sfcontainer` attribute are available for adding widgets.

### Create a page template based on the layout file

You can base Sitefinity CMS page templates on the layout files from the Renderer. This way, you define the common structure once, in the layout file, and add the common widgets, such as the header navigation and the footer, to the page template in the New page editor.

To create a page template based on your layout file, follow the procedure in [Create custom templates in the backend](https://www.progress.com/documentation/sitefinity-cms/create-custom-templates-in-the-backend). On the *Select a base template* page, select your layout file under *.NET Core templates*. For more information, see [ASP.NET Core pages and templates](https://www.progress.com/documentation/sitefinity-cms/create-an-asp.net-core-page).

## Sample project

For a complete working project, see the [Custom layout](https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-samples/tree/master/src/custom-layout) sample on Sitefinity GitHub repository.

For a layout file with multiple placeholders and edit-mode styles, see the [Quantum](https://github.com/Sitefinity/sitefinity-aspnetcore-mvc-samples/tree/master/src/quantum) sample on Sitefinity GitHub repository.
