Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 41 ยท Components and Razor Syntax

Child Content and Render Fragments

Build wrapper and templated components in Blazor with ChildContent, RenderFragment, RenderFragment of T, named fragments and generic components using @typeparam, with runnable C# stand-ins.

Parameters pass values. But sometimes you want to pass markup: a card whose body is whatever the caller puts inside it, a table whose rows are drawn by the caller, a list that renders each item in the caller's own way. Blazor handles this with render fragments. They are one of the most powerful ideas in the framework, and they are simple once you see the shape.

ChildContent

A parameter of type RenderFragment named ChildContent receives whatever is written between a component's opening and closing tags:

HTML
@* Components/Card.razor *@
<div class="card">
    <div class="card-header">@Title</div>
    <div class="card-body">@ChildContent</div>
</div>

@code {
    [Parameter] public string Title { get; set; } = "";
    [Parameter] public RenderFragment? ChildContent { get; set; }
}
HTML
<Card Title="Welcome">
    <p>This paragraph is the child content.</p>
    <button @onclick="Start">Get started</button>
</Card>

The name ChildContent is special: Blazor fills it with the content between the tags without an explicit attribute. Writing @ChildContent in the component draws the fragment at that spot. If the caller passes nothing, the property is null, so @ChildContent safely draws nothing.

Event handlers inside the fragment, like Start, belong to the parent that wrote them, even though the markup is displayed inside Card.

Several named fragments

You can have more than one fragment parameter and fill them by tag name:

HTML
@* Components/Panel.razor *@
<section>
    <header>@Header</header>
    <div>@Body</div>
    <footer>@Footer</footer>
</section>

@code {
    [Parameter] public RenderFragment? Header { get; set; }
    [Parameter] public RenderFragment? Body { get; set; }
    [Parameter] public RenderFragment? Footer { get; set; }
}
HTML
<Panel>
    <Header><h2>Settings</h2></Header>
    <Body><p>Choose your options.</p></Body>
    <Footer><small>Saved automatically</small></Footer>
</Panel>

When you use named fragments this way, everything inside the component tag must be one of them (stray content goes to ChildContent, which then needs to exist).

Templates with RenderFragment of T

RenderFragment<T> is a fragment that takes a value. The component supplies the value; the caller decides how to draw it. That is how list and table components let you customise rows:

HTML
@* Components/ItemList.razor *@
<ul>
    @foreach (var item in Items)
    {
        <li>@ItemTemplate(item)</li>
    }
</ul>

@code {
    [Parameter] public IEnumerable<string> Items { get; set; } = [];
    [Parameter] public RenderFragment<string> ItemTemplate { get; set; } = default!;
}
HTML
<ItemList Items="names">
    <ItemTemplate Context="name">
        <strong>@name.ToUpper()</strong>
    </ItemTemplate>
</ItemList>

Context names the variable that holds the item inside the template. Without it, the variable is called context. Think of RenderFragment<T> as "a function from T to markup".

Generic components

Pair templates with @typeparam and the component works for any item type:

HTML
@typeparam TItem

<table>
    <thead><tr>@Header</tr></thead>
    <tbody>
        @foreach (var item in Items)
        {
            <tr>@RowTemplate(item)</tr>
        }
    </tbody>
</table>

@code {
    [Parameter] public IReadOnlyList<TItem> Items { get; set; } = [];
    [Parameter] public RenderFragment? Header { get; set; }
    [Parameter] public RenderFragment<TItem> RowTemplate { get; set; } = default!;
}

Blazor infers TItem from Items, so <DataTable Items="products"> makes the template's context a Product. This is the basis of grids, dropdowns and many component libraries.

What a RenderFragment really is

RenderFragment is simply a delegate: void RenderFragment(RenderTreeBuilder builder). A fragment is "code that, when called, writes into a render tree". That is why you can build one by hand:

C#
RenderFragment hello = builder => builder.AddContent(0, "Hello");

And RenderFragment<T> is a Func<T, RenderFragment>. You can also store a fragment in a field using Razor syntax:

HTML
@{ RenderFragment badge = @<span class="badge">New</span>; }
@badge

A runnable stand-in: fragments as functions

In plain C#, a fragment is a function that appends markup to a builder. These classes mirror the Card, ItemList and generic DataTable idea.

C# Stand-in: render fragments as delegates

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

DataTable never mentions Product: the caller's row function knows how to draw one. Change the caller and the same component draws users, orders or anything else.

Tips

  • Always declare fragments as nullable (RenderFragment?) unless they are required.
  • Use ChildContent for "wrapper" components such as cards, dialogs, tabs and error boundaries.
  • Use RenderFragment<T> when the component owns the data and the caller owns the presentation.
  • Keep component styles in scoped CSS next to the file so wrappers stay self-contained.

Common mistakes

  • Naming a fragment parameter something other than ChildContent and expecting implicit content. Only the exact name ChildContent receives the inner markup.
  • Mixing loose markup and named fragments inside a component tag.
  • Using context in a nested template inside another template. Name the inner variable with Context="..." to avoid clashes.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which parameter receives the markup written between a component's start and end tags?

    A RenderFragment parameter named ChildContent receives the inner markup.
  2. 2. What is RenderFragment<T> best described as?

    The component supplies a T and the caller's template decides how to draw it.
  3. 3. What does Context="item" on a template tag do?

    Without Context the variable is named context; Context renames it.
  4. 4. Which directive lets a component work with any item type?

    @typeparam declares a generic type parameter for the component, usually inferred from a parameter.

Summary

  • RenderFragment? ChildContent receives the markup between a component's tags.
  • Named RenderFragment parameters create multiple slots such as header, body and footer.
  • RenderFragment<T> plus Context lets callers customise how each item is drawn.
  • @typeparam makes templated components generic.
  • A fragment is just a delegate that writes to a render tree.

Next: Component Lifecycle