---
title: Dynamic Templates that can be repeated easily
description: How to create dynamic grid column templates for easy reuse and dynamic logic
type: how-to
page_title: Dynamic Column Templates that can be repeated easily
slug: grid-kb-dynamic-columns-templates
position:
tags:
ticketid: 1496723
res_type: kb
---
## Environment
Product
Grid for Blazor,
TreeList for Blazor,
UI for Blazor
## Description
When working with templated columns some code is repetitive for each Column that needs to be in a template (we have a lot of templates in each grid) and I want an easier and more reusable way to write templates.
How can I define my template code in C# so it is easier to reuse?
I have dynamic objects with many fields and I want to pass the object and field name to the template so I can extract the information I need.
## Solution
While all of this is possible with templates in the markup, some code may get repetitive (especially casting the `context`) and/or may be harder to debug. You can extract templates to the C# portion of your component by defining the `RenderFragment` object yourself.
Another option is, of course, to create a child component that receives the `context` and provides the required casting and rendering on its own. This is a well documented approach in the Blazor framework so this article will focus on the specific way of writing a `RenderFragment`.
>caption Using a `RenderFragment` in the C# code to create a template by passing a field name to it so you can extract particular information based on that field with Reflection.
````CSHTML
@using System.Reflection;
@code {
List forecasts { get; set; }
protected override void OnInitialized()
{
GetForecasts();
}
void GetForecasts()
{
forecasts = Enumerable.Range(1, 20).Select(x => new WeatherForecast()
{
Id = x,
Date = DateTime.Now.AddDays(x),
Summary = "Summary" + x
}).ToList();
}
public class WeatherForecast
{
public int Id { get; set; }
public DateTime Date { get; set; }
public string Summary { get; set; }
}
public RenderFragment