# Form Template for All Items

This article describes how to create custom Form layouts with components and HTML markup between or around the Form items. To define a custom editor in a specific Form item, see article [Form Item Template](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/template).

> tip The content on this page applies to Telerik UI for Blazor version **4.2.0** and above.

## Background

The `<FormItems>` tag is a standard Blazor `RenderFragment`, which expects only [Form groups](https://www.telerik.com/blazor-ui/documentation/components/form/formgroups) and [Form items](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview). The Blazor framework does not support tag restrictions inside `RenderFragment`s. As a result, `<FormItems>` allows non-Form child components and arbitrary markup. However, such setup with custom markup was never officially supported before version 4.2.0. Here is the relevant Form behavior timeline:

1. Until **version 4.0.1** Form items and groups could be mixed with arbitrary content. This worked without explicit intent or support.
2. **Version 4.1.0** added the ability to [combine auto-generated and declared Form items](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview#add-form-fields-to-autogenerated-ones). The feature required changes in the Form implementation. As a result, custom components inside `<FormItems>` caused unexpected Form item reordering.
3. **Version 4.2.0** added official support for custom markup and components inside the Form.

## Using Template for All Form Items

Telerik UI for Blazor version 4.2.0 introduced one new Form parameter and two new components that allow mixing Form groups and items with other content, such as HTML markup or Razor components:

<style>
    .article table {
        table-layout: auto;
        word-break: normal;
    }
</style>

| Feature                                                                                                                                         | Description                                                                                                                                     |
| ----------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| [`FormItemsTemplate`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#formitemstemplate)            | A `RenderFragment` parameter of the `TelerikForm` component, so it's usually used as a child tag.                                               |
| [`<TelerikFormGroupRenderer>`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#form-group-renderer) | A component that renders a specific *Form group*. It can occur anywhere inside `<FormItemsTemplate>`.                                           |
| [`<TelerikFormItemRenderer>`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#form-item-renderer)   | A component that renders a specific *Form item*. It can occur inside `<FormItemsTemplate>` or the `Template` of a `<TelerikFormGroupRenderer>`. |

The Form setup uses the following architecture:

* [`<FormItems>`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview) defines the **configuration** of all Form groups and items, including the [`<Template>`s of the form items](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/template).
* [`<FormItemsTemplate>`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#formitemstemplate) is the optional **rendering container** of all groups and items.
* [`<TelerikFormGroupRenderer>`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#form-group-renderer) defines the **rendering place** of a specific form **group**.
* [`<TelerikFormItemRenderer>`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#form-item-renderer) defines the **rendering place** of a specific form **item**.

> As any other template, `FormItemsTemplate` overrides the built-in rendering of Form groups and items. When the `<FormItemsTemplate>` tag is used, the Form component will render only groups and items that have their own *renderer* components. The HTML output *inside* each form item is unrelated to this feature and is not affected. [Form `Columns`](https://www.telerik.com/blazor-ui/documentation/components/form/columns) and [FormItem `ColSpan`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview#formitem-parameters) may work differently or stop working, depending on the exact template markup. In such cases, stop using the built-in Form columns and achieve the desired layout with custom HTML and CSS.

## FormItemsTemplate

The `FormItemsTemplate` `RenderFragment` provides a context of type `FormItemsTemplateContext`. This object exposes an `Items` property, which is a generic `List<IFormItemBase>` and can contain form groups (`IFormGroup`) and form items (`IFormItem`).

Be aware of [nested form contexts](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#nested-form-contexts) when using `TelerikFormGroupRenderer`.

## Form Group Renderer

The `TelerikFormGroupRenderer` component defines the rendering place of a Form group inside the `FormItemsTemplate`. The component has the following parameters:

| Parameter  | Type             | Description                                                                                                                                                                                                                                                                                                                                            |
| ---------- | ---------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `Group`    | `IFormGroup`     | A Form group, which is defined in `<FormItems>`. The `IFormGroup` interface provides all properties of a `<FormGroup>` component, so you can access and reuse the group configuration, which is defined in `<FormItems>`. `IFormGroup` also implements an `Items` property, which is a `List<IFormItemBase>` and provides access to nested Form items. |
| `Template` | `RenderFragment` | The container for the Form group content. The fragment exposes a `context`, which is an `IFormGroup`.                                                                                                                                                                                                                                                  |

### Nested Form Contexts

The `TelerikFormGroupRenderer` `Template` is nested inside another template - `FormItemsTemplate`. Both templates expose a `context`, so **at least one of these contexts must have a custom name**, which is set via the template `Context` parameter. Otherwise you will get an [error about child content using the same parameter name](https://www.telerik.com/blazor-ui/documentation/knowledge-base/nest-render-fragment). Check the the snippet below and the [Render Form items in a loop](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#render-form-groups-and-items-in-a-loop) example.

> caption Setting named contexts in the Form templates

```RAZOR.skip-repl
<TelerikForm>
    <FormItemsTemplate Context="formContext">
        ...
        <TelerikFormGroupRenderer>
            <Template Context="groupContext">
                ...
            </Template>
        </TelerikFormGroupRenderer>
        ...
    </FormItemsTemplate>
<TelerikForm>
```

## Form Item Renderer

The `TelerikFormItemRenderer` component defines the rendering place of a Form item inside the `FormItemsTemplate`. The component has the following parameters:

| Parameter | Type        | Description                                                                                                                                                                                                                             |
| --------- | ----------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Item`    | `IFormItem` | A Form item, which is auto-generated or defined in `<FormItems>`. The `IFormItem` interface provides all properties of a `<FormItem>` component, so you can access and reuse the item configuration, which is defined in `<FormItems>`. |

> tip `TelerikFormItemRenderer` is unrelated to the [template of a single `<FormItem>`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/template). The two features are used separately and independently.

## Upgrading from Older Versions

Here are the steps to migrate custom content inside the Form from version 4.0.1- to 4.2.0+.

1. Move all custom components and markup from `<FormItems>` to `<FormItemsTemplate>`. The `<FormItems>` tag should hold only `<FormItem>` and `<FormGroup>` tags.
2. Inside `<FormItemsTemplate>`, define a `<TelerikFormGroupRenderer>` and `<TelerikFormItemRenderer>` for each group and item that exists in `<FormItems>`. Place each Renderer tag at the desired place. This steps also applies to `<FormAutoGeneratedItems />`, which need Renderers too.
3. If using conditional statements to render `<FormItem>` tags, then define these tags unconditionally. Instead, use conditional logic for `<TelerikFormItemRenderer>` tags inside `<FormItemsTemplate>`.
4. If [using `<FormItem>` instances inside a nested Razor component](https://www.telerik.com/blazor-ui/documentation/knowledge-base/form-use-items-in-custom-component), then see the linked KB article and:
   * Move these `<FormItem>` tags inside the Form `<FormItems>` tag.
   * Pass the `FormItemsTemplate` `context` or a specific `IFormItem` as a new parameter of the nested component.
   * Use `<TelerikFormItemRenderer>` inside the nested component. Consume the new `IFormItem` parameter in the `Item` parameter of the Renderer.

> caption Comparison between versions up to 4.0.1 and after 4.2.0.

<table>
<thead><tr><th>Up to 4.0.1</th><th>From 4.2.0</th></tr></thead>
<tbody><tr>
<td>
<p><strong>Index.razor</strong></p>

```RAZOR.skip-repl
<TelerikForm>
  <FormItems>
    <div>
      content around form items
      <FormItem Field="FieldName1" />
    </div>
    <div>content between items<div>
    <FormItem Field="FieldName2">
      <Template>
        ...
      </Template>
    </FormItem>
    @if (someCondition)
    {
        <FormItem Field="FieldName3" />
    }
    <ChildComponent />
  </FormItems>

























</TelerikForm>
```

</td>
<td>
<p><strong>Index.razor</strong></p>

```RAZOR.skip-repl
<TelerikForm>
  <FormItems>


    <FormItem Field="FieldName1" />


    <FormItem Field="FieldName2">
      <Template>
        ...
      </Template>
    </FormItem>


    <FormItem Field="FieldName3" />

    <FormItem Field="FieldName4" />
  </FormItems>
  <FormItemsTemplate Context="formContext">
    @{
      var formItems = formContext.Items
        .Cast<IFormItem>().ToList();
    }
    <div>
      content around form items
      <TelerikFormItemRenderer
          Item="@( formItems.First(x =>
            x.Field == "FieldName1") )" />
    </div>
    <div>content between items<div>
    <TelerikFormItemRenderer
      Item="@( formItems.First(x =>
        x.Field == "FieldName2") )" />
    @if (someCondition)
    {
        <TelerikFormItemRenderer
        Item="@( formItems.First(x =>
            x.Field == "FieldName3") )" />
    }
    @{ var formItem4 = formItems.First(x =>
        x.Field == "FieldName4"); }
    <ChildComponent NestedFormItem="@formItem4" />
  </FormItemsTemplate>
</TelerikForm>
```

</td>
</tr>
<tr>
<td>
<p><strong>ChildComponent.razor</strong></p>

```RAZOR.skip-repl
<FormItem Field="FieldName4" />







```

</td>
<td>
<p><strong>ChildComponent.razor</strong></p>

```RAZOR.skip-repl
<TelerikFormItemRenderer Item="@NestedFormItem" />

@code {
    [Parameter]
    public IFormItem NestedFormItem { get; set; }
}
```

</td>
</tr>
</tbody>
</table>

## Examples

The samples below demonstrate a few different use cases:

* [Render Form groups and items in a loop](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#render-form-groups-and-items-in-a-loop)
* [Render Form items one by one or conditionally](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#render-form-items-one-by-one-or-conditionally)
* [Combine autogenerated and defined Form items](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/formitemstemplate#combine-autogenerated-and-defined-form-items)

Also check the following examples on other pages:

* [How to insert Form items in a custom Razor component inside the Form](https://www.telerik.com/blazor-ui/documentation/knowledge-base/form-use-items-in-custom-component)
* [Form Templates online demo](https://demos.telerik.com/blazor-ui/form/templates).

### Render Form Groups and Items in a Loop

This approach is suitable for scenarios where the custom Form layout can accommodate a random number of groups or items.

> tip If the Form has no groups, then the `formContext.Items` `List` will contains only `IFormItem` instances. In this case, there is no need to check the type of each `List` member, but you will still need to cast the member from `IFormItemBase` to `IFormItem`.

> caption Use a loop to render Form groups and items inside a FormItemsTemplate

<demo metaUrl="client/form/formitemstemplate/example-3/" height="420"></demo>

### Render Form Items One by One or Conditionally

This approach is suitable for scenarios where the application positions each Form item (or group) at a specific place in the `FormItemsTemplate`.

`FormItemsTemplateContext.Items` is a generic `List<IFormItemBase>`, so there are [multiple different ways to get each form item (or group) via standard methods](https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1). The example below shows a few alternatives - `First()`, `Skip()`, and index.

The sample also demonstrates how to display Form items and groups conditionally, depending on the value of another Form field (`BirthDate`). Note the usage of a [Form item `Template`](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/template) for `BirthDate`, which enables the Form to re-render and toggle the `DriversLicense`, `Team`, and `Salary` fields. See [UI rendering and performance optimization inside the Form](https://www.telerik.com/blazor-ui/documentation/components/form/formitems/overview#ui-rendering-inside-the-form) for more ways to refresh the Form component.

> caption Render defined Form items inside a FormItemsTemplate

<demo metaUrl="client/form/formitemstemplate/example-2/" height="420"></demo>

### Combine Autogenerated and Defined Form Items

If used, the optional `<FormAutoGeneratedItems />` tag belongs to `<FormItems>`. Then, get and render the auto-generated form items inside `<FormItemsTemplate>` in the same way as explicitly defined `<FormItem>` instances.

When using only auto-generated Form items with a custom Form layout, you can remove the whole `<FormItems>` tag and only use `<FormItemsTemplate>`.

> caption Render auto-generated and defined Form items inside a FormItemsTemplate

<demo metaUrl="client/form/formitemstemplate/example-1/" height="420"></demo>

## See Also

* [Online Demo: Form Templates](https://demos.telerik.com/blazor-ui/form/templates)
