How to Use Templated Components in Blazor
Table of Contents
What is a templated component?
Templated components are components that accept one or more UI templates as parameters. This is then used as part of the rendering logic. This allows users to author higher-level components that are highly reusable.
I am going to cover a list component that allows a user to specify a template for items in a list for rendering.
Generic typed components
- Templated components are generically typed often
- Usage of @typeparam directive is made for defining a generic component
Now lets switch over to Visual Studio and start the coding. I created a Blazor Web Assembly project named GetStartedBlazor without ASP.Net Core hosting.
Within the GetStartedBlazor project, create Customer.cs file at the root of the project and paste the following code for the Customer class.
Customer.cs
namespace GetStartedBlazor
{
public class Customer
{
public string Name { get; set; }
public int Age { get; set; }
public Customer(string name, int age)
{
Name = name;
Age = age;
}
}
}
Create ListViewTemplate razor component
Now within the Pages folder, create a razor component called ListViewtemplate and paste the following code to replace the existing code
ListViewTemplate.razor
@typeparam TItem
<ul>
@foreach (var item in Items)
{
@ItemTemplate(item)
}
</ul>
@code {
[Parameter]
public RenderFragment<TItem> ItemTemplate { get; set; }
[Parameter]
public IReadOnlyList<TItem> Items { get; set; }
}
In the above code block, TItem is a generic component, so it means that ItemTemplate returns a RenderFragment of generic component type and Items is a readonly list of generic components. The foreach loop iterates over the readonly list of items and renders a fragment with a given item template. [Parameter] is used to mark the component parameters that can be set when the component is used on another page which we shall see below. A render fragment represents a segment of UI to render. RenderFragment<TValue> takes a type parameter that can be specified when the render fragment is invoked.
Create ListViewTemplateUser razor component to render the list view
Create another razor component named ListViewTemplateUser which uses the ListViewTemplate. Paste the following code to replace the template code in the razor component.
ListViewTemplateUser.razor
@page "/LVTU"
<div>
<ListViewTemplate Items="customers">
<ItemTemplate Context="customer">
<p> <li>Name:@customer.Name</li></p>
<p> <li>Age: @customer.Age</li></p>
</ItemTemplate>
</ListViewTemplate>
</div>
@code
{
[Parameter]
public IReadOnlyList<Customer> customers { get; set; } =
new List<Customer>()
{
new Customer("John",56),
new Customer("Sandy", 34),
new Customer("Andrew", 45),
new Customer("Emily", 47)
};
}
In the code above, the @page directive allows navigation of the ListViewTemplateUser with the URL: https://localhost:port/LVTU. ListViewTemplateUser is the parent component that uses the ListViewTemplate component on the list of Customers and renders the list with the Customer Name and Age on the browser
Run the application
Finally, if we build and run the application and browse as follows, we get the following view:

YouTube Version of this Post
The YouTube version of this blog post is available on my channel here.
Follow Me Here