---
title: "Pass HTML into named component slots in C#"
url: https://recursiveai.org/oneui/cookbook/pass-html-content-to-component-slots/
description: "Define a named slot with fallback HTML in OneUI. Fill it from a parent template, leave it empty in another instance, and update the parent’s bound title."
version: "1.0.0"
---

# Pass HTML into named component slots in C#

**OneUI** is a free, open-source .NET library from Recursive AI for writing UI components in HTML, CSS, and C# that run in desktop windows or in a browser via WebAssembly with OneApp. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneUI) · [GitHub](https://github.com/recursive-ai-oss/oneui-csharp) · [Docs](https://recursiveai.org/oneui/docs/) · [Download example (.zip)](https://recursiveai.org/oneui/cookbook/pass-html-content-to-component-slots/pass-html-content-to-component-slots.zip)

Declare the slot in the child. Fill it inside `<InfoCard>` in the parent.

```shell
dotnet add package RecursiveAI.OneUI
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

`InfoCard.html`:

```html
<slot name="title">Details</slot>
```

`CardPage.html` (inside `<InfoCard>`):

```html
<template slot="title">
  <strong>{Heading}</strong>
</template>
```

[The example](https://recursiveai.org/oneui/cookbook/pass-html-content-to-component-slots/pass-html-content-to-component-slots.zip) is the full program below: unzip it and `dotnet run`.

The first card uses the parent’s bold heading; the second displays the child’s fallback, “Details.”

## The full program

Pair `InfoCard.html` with `InfoCard.cs` and `CardPage.html` with `CardPage.cs`: each template and partial class share a name and folder. OneUI generates the component code and the state property implementation. The console app prints both cards, changes the parent’s heading, then prints the first heading again.

`InfoCard.html`:

```html
<section>
  <h2><slot name="title">Details</slot></h2>
  <p><slot /></p>
</section>
```

`CardPage.html`:

```html
<InfoCard>
  <template slot="title">
    <strong>{Heading}</strong>
  </template>
  Order #42
</InfoCard>
<InfoCard>Order #43</InfoCard>
```

`InfoCard.cs`:

```csharp
public partial class InfoCard { }
```

`CardPage.cs`:

```csharp
using OneUI.Components;

public partial class CardPage
{
    [State]
    public partial string Heading { get; set; }
    public CardPage() => Heading = "Packing";
}
```

`Program.cs`:

```csharp
using OneUI.Components;
using OneUI.Components.Reference;

var doc = new ReferenceDocument();
var ui = new UiContext(doc);
doc.Context = ui;
var page = new CardPage();
ui.Mount(page);

Console.WriteLine(doc.ToHtml());
page.Heading = "Shipped";
Console.WriteLine(doc.Text("h2"));
```

`dotnet run` prints:

```text
<section><h2><strong>Packing</strong></h2><p>Order #42</p></section><section><h2>Details</h2><p>Order #43</p></section>
Shipped
```

A misspelled slot name in the parent is a build error, rather than an instruction to use the child’s fallback.

## Variations

To use the fallback for both cards, replace `CardPage.html` with this version. Both headings now say `Details`, even after the parent changes `Heading`:

```html
<InfoCard>Order #42</InfoCard>
<InfoCard>Order #43</InfoCard>
```

## Related recipes

- [Scope CSS to an HTML component](https://recursiveai.org/oneui/cookbook/scope-css-to-component/)
- [Build a UI component without HTML templates](https://recursiveai.org/oneui/cookbook/build-ui-component-without-markup/)

## The short answer

Declare a placeholder in the child with `<slot name="title">Details</slot>`. In the parent, put a `<template slot="title">` block inside the child’s component tag. Its contents replace `Details` for that instance. The unnamed `<slot />` receives the content outside any named template block, here the order number.

## What’s the difference between a named slot and a default slot?

Use a named slot when content needs to go in a specific part of a component, such as the heading or a footer. Use the default slot for the main content. The child decides where both go. In this example the child owns the `<h2>`, so the parent supplies only what goes *inside* it. A second card can use the same layout without supplying a custom heading at all. The slot instructions are compiled by OneUI; the result doesn’t depend on a browser assigning nodes to slots at runtime.

## Can content in a slot read the parent’s state?

Yes. The child decides where the markup appears, but bindings in that markup read the state of the component that declared it. `Heading` belongs to `CardPage`, not `InfoCard`, so changing `CardPage.Heading` updates the supplied `<strong>` without changing the fallback in the other card.

Use a `[Param]` when the parent only needs to pass a value and the child should decide how to present it. Use a slot when the parent needs to choose the markup itself. The [markup docs](https://recursiveai.org/oneui/docs/markup/#template-syntax) show both forms.
