# Using Client Templates

The Telerik UI for ASP.NET MVC components provide templating options that let you customize and control their appearance or integrate other UI components. Many use cases require displaying a specified component element (for example, item, option, button, cell, and more) differently based on a specified condition or overriding its default look and feel. In such scenarios, client templates come in handy.

The available template types are:

* [Inline Client Templates](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#inline-client-templates)
* [External Client Templates](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#external-client-templates)
* [Partial Client Templates](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#partial-client-templates)
* [Content Security Policy Templates](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#content-security-policy-csp-templates)
* [How exactly template parameter evaluation works](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#how-exactly-template-parameter-evaluation-works)

## Inline Client Templates

You can create an inline client template as a `string` or a `JavaScript` function. They are suitable for small templates.

The following example demonstrates how to define the [template for the DropDownList items](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/dropdownlist/templates) as a `string`. To display the Model properties values as HTML, use the [hash syntax (`#=#`)](https://docs.telerik.com/kendo-ui/framework/templates/essentials#template-syntax).

```Razor HtmlHelper
    @(Html.Kendo().DropDownList()
        .Name("customers")
        .HtmlAttributes(new { style = "width: 100%" })
        .DataTextField("ContactName")
        .DataValueField("CustomerID")
        .Template("<span class=\"contact-name\" style=\"color: \\#ffa96a;\"><b>#=ContactName #</b><p>#=CompanyName#</p></span>") // You can display the values of Model properties that are available in the data returned from the Read request.
        .DataSource(source =>
        {
            source.Read(read =>
            {
                read.Action("Template_GetCustomers", "DropDownList");
            });
        })
        .Height(400)
    )
```

To set an inline template as a function, pass the name of the function in the template method.

```Razor HtmlHelper
    @(Html.Kendo().DropDownList()
        .Name("customers")
        .HtmlAttributes(new { style = "width: 100%" })
        .DataTextField("ContactName")
        .DataValueField("CustomerID")
        .Template("#=getDropDownItem(data)#")
        .DataSource(source =>
        {
            source.Read(read =>
            {
                read.Action("Template_GetCustomers", "DropDownList");
            });
        })
        .Height(400)
    )

    <script>
        function getDropDownItem(data) {
            if (data.Country == "Germany") {
                let templateString = ({ ContactName }) => `<span style="color: orange;"><b> ${kendo.htmlEncode(ContactName)}</b></span>`;
                let template = kendo.template(templateString);
                return template(data);
            } else {
                let templateString = ({ ContactName, CompanyName }) => `<span style="color: blue;"><b> ${kendo.htmlEncode(ContactName)}</b> --> ID: <i>${kendo.htmlEncode(CompanyName)}</i></span>`;
                let template = kendo.template(templateString);
                return template(data);
            }
        }
    </script>
```

## External Client Templates

The external client templates are preferable for more advanced and complex scenarios. Within an external template, you can add HTML markup and JavaScript logic, as long as the JavaScript is properly formatted with the [Kendo UI Template syntax](https://docs.telerik.com/kendo-ui/framework/templates/essentials#handling-external-templates-and-expressions).

The external template is a Kendo UI Template defined by using HTML script blocks, which is suitable for larger templates.

For example, you can define an external client template for a [Grid column](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/columns/templates) by following the steps below:

1. Set up a script block with the `text/x-kendo-template` type.

   ```JS
       <script type="text/x-kendo-template">
           
       </script>
   ```

2. Add an `id` to the template script and ensure its value is unique on the page.

   ```JS
       <script type="text/x-kendo-template" id="myTemplate">

       </script>
   ```

3. Specify the desired JavaScript logic in the template. For example, display the relevant information based on the value of the `Discontinued` property.

   ```JS
       <script type="text/x-kendo-template" id="myTemplate">
           <div class="product-item">
           #if(Discontinued){#
               <div><b>Active order</b></div>
           #}else{#
               <div>#: ProductName # is already shipped</div>
           #}#
           </div>
       </script>
   ```

4. Pass the `id` of the template to the `ClientTemplateId` method of the Grid column.

   ```Razor HtmlHelper
       @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
           .Name("Grid")
           .Columns(columns =>
           {
               columns.Bound(p => p.ProductName);
               columns.Bound(p => p.UnitPrice);
               columns.Bound(p => p.Discontinued).ClientTemplateId("myTemplate");
           })
           .Pageable()
           .Scrollable()
           .DataSource(dataSource => dataSource
               .Ajax()
               .PageSize(20)
               .Read("Products_Read", "Grid")
           )
       )
   ```

Also, you can integrate Telerik UI for ASP.NET MVC components in the external client templates by using the [HTML Helpers](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#adding-html-helpers-inside-external-client-templates) .

### Adding HTML Helpers inside External Client Templates

By default, [every Telerik UI HtmlHelper renders a `script` element immediately after its HTML markup](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/deferred-initialization). If the helper declaration is placed inside an external Kendo UI template, the nested script elements will be invalid. The `ToClientTemplate` method instructs the helper to escape its own script element so that it can be nested.

```
<script id="template" type="text/x-kendo-template">
    @(Html.Kendo().NumericTextBox()
          .Name("age")
          .ToClientTemplate()
    )
</script>
<div id="container"></div>
<script>
    $(function () {
       var template = kendo.template($("#template").html());
       $("#container").append(template);
    })
</script>
```

## Partial Client Templates

As of the R1 SP1 2023 release, the majority of the Telerik UI for ASP.NET MVC components expose the ability to include arbitrary client template content within the boundaries of a Partial View by using the respective component's new `TemplateView` method.

The example below illustrates how to incorporate the Grid Toolbar's template content within a Partial View.

```Razor HtmlHelper
    @(Html.Kendo().Grid<Kendo.Mvc.Examples.Models.ProductViewModel>()
        .Name("grid")
        .Columns(columns => {
            columns.Bound(p => p.ProductID).Visible(true).Width(100);
            columns.Bound(p => p.ProductName);
            columns.Bound(p => p.UnitPrice).Width(150);
            columns.Bound(p => p.QuantityPerUnit);
        })
        .ToolBar(toolbar => {
            toolbar.ClientTemplateView(Html.Partial("_ToolbarTemplatePartial"));
        })
        .DataSource(dataSource => dataSource
            .Ajax()
            .ServerOperation(false)
            .Model(model => model.Id(p => p.ProductID))
            .Read("ToolbarTemplate_Read", "Grid")
        )
    )
```

```Razor _Partial.cshtml
    <div class="toolbar">
     <label class="category-label" for="category">Show products by category:</label>
     @(Html.Kendo().DropDownList()
         .Name("categories")
         .OptionLabel("All")
         .DataTextField("CategoryName")
         .DataValueField("CategoryID")
         .HtmlAttributes(new { style = "width: 150px;" })
         .DataSource(ds =>
         {
             ds.Read("ToolbarTemplate_Categories", "Grid");
         })
         .ToClientTemplate()
     )
    </div>
```

## Content Security Policy (CSP) Templates

As of the R1 SP1 2023 release, Telerik UI for ASP.NET MVC addresses the [content security policy issues](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/content-security-policy) related to the `unsafe-eval` directive for components except for the Spreadsheet.

To remove the `unsafe-eval` keyword from the meta tag of your application, you must convert all client templates ([inline](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#inline-client-templates), [external](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#external-client-templates), and [partial](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#partial-client-templates) templates) into CSP-compatible templates.

To create CSP-compatible templates, use any of the following approaches:

* [Using Template component](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#using-the-template-component)
* [Using Client-Side handler](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/using-client-templates#using-client-side-handler)

### Using the Template Component

As of R2 2023 release, you can take advantage of the [Template component](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/template/overview) that provides options for integrating a variety of Telerik UI for ASP.NET MVC components and `HTML` code into the templates of the components. You can declare the desired UI component into the Template component configuration and use all options as they are available in the nested component itself.

In the preceding code:

* The `AddHtml()` of the Template component inserts `HTML` into the column template of the TreeList. The `Title` is a property of the `Model` that binds to the TreeList.
* The `TemplateComponentName()` defines the name of a custom Popup editor template, which is configured through the Template component.

```Razor HtmlHelper
    @(Html.Kendo().TreeList<EmployeeViewModel>()
        .Name("treelist")
        .Columns(columns =>
        {
            columns.Add().Field(e => e.Name)
                .Template(Html.Kendo().Template().AddHtml("<span style='color: green;'>${data.Name} - ${data.Title}</span>"));
        })
        .Editable(e => e.Mode("popup").TemplateComponentName("CustomPopupEditor"))
        //Other configuration
    )
```

```Razor CustomPopupEditor.cshtml
    //~/Views/Shared/EditorTemplates/CustomPopupEditor.cshtml

    @model EmployeeViewModel

    @(Html.Kendo().Template()
        .AddHtml("<div class=\"k-edit-field\">")
        .AddComponent(tbox => tbox
            .TextBoxFor(model => model.Name)
            .Label(l => l.Content("Full name:").Floating(true))
        )
        .AddHtml("</div>")
    )
```

### Using Client-Side Handler

Telerik UI for ASP.NET MVC introduces an overload of the components template methods that accept a `JavaScript` function name. It allows you to define the template content through a client-side handler. This way, you can prevent the components from being dependent on the `unsafe-eval` and reuse the templates within multiple components in different application pages.

The example below demonstrates how to load the [item template of a ComboBox](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/editors/combobox/templates#item-template) through a function handler.

```Razor HtmlHelper
    @(Html.Kendo().ComboBox()
        .Name("customers")
        .DataTextField("ContactName")
        .DataValueField("CustomerID")
        .HtmlAttributes(new { style = "width: 100%;" })
        .DataSource(source =>
        {
            source.Read(read =>
            {
                read.Action("Template_GetCustomers", "ComboBox");
            });
        })
        .TemplateHandler("itemTemplateHandler")
    )
```

```JS scripts.js
    function itemTemplateHandler(data) {
        if(data.Country == "Germany"){
            return `<div><b>${data.ContactName}</b></div>`;
        } else {
            return `<div><i>${data.ContactTitle}</i> - <b>${data.ContactName}</b></div>`;
        }
    }
```

## How exactly template parameter evaluation works

The templating options use [Kendo UI Templates](https://docs.telerik.com/kendo-ui/framework/templates/overview), which offer a way to create HTML chunks that can be merged automatically with JavaScript code. They are a substitute for traditional HTML string-building in JavaScript.

> To display the values of the Model properties as HTML, use the [hash syntax (`#=#`)](https://docs.telerik.com/kendo-ui/framework/templates/essentials#template-syntax).

The `data` object, available in the template, varies depending on the component and the template type. For example, the [Grid Column FooterTemplate](https://docs.telerik.com/kendo-ui/api/ui/grid/configuration/columns.footertemplate) exposes the aggregates data for the specific column, while the [Chart Series TooltipTemplate](https://docs.telerik.com/kendo-ui/api/dataviz/ui/chart/configuration/series.tooltip.template) exposes the `category`, `value`, `series`, and `dataItem` objects.

For more information on the data available in the different templates, refer to the [Kendo UI for jQuery API documentation](https://docs.telerik.com/kendo-ui/api/introduction) or search the [Knowledge Base](https://docs.telerik.com/kendo-ui/knowledge-base).

## See Also

* [Content Security Policy](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/helper-basics/content-security-policy)
* [Template Overview](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/template/overview)
* [Kendo UI Templates Overview](https://docs.telerik.com/kendo-ui/framework/templates/overview)
* [Getting Started with the Kendo UI Inline Templates](https://docs.telerik.com/kendo-ui/framework/templates/get-started-inline)
* [Getting Started with Kendo UI External Templates](https://docs.telerik.com/kendo-ui/framework/templates/get-started-external)
* [Getting Started with Kendo UI Content Security Policy (CSP) Templates](https://docs.telerik.com/kendo-ui/framework/templates/get-started-csp-templates)
* [Known Exceptions: Limited Usage of Templates](https://www.telerik.com/aspnet-mvc/documentation/html-helpers/data-management/grid/troubleshoot/known-exceptions#limited-usage-of-templates)
