---
title: "Conditionally render HTML in a C# component template"
url: https://recursiveai.org/oneui/cookbook/render-conditional-content-in-template/
description: "Conditionally render HTML in a C# component template with OneUI. Use if and else branches to switch between saved and unsaved messages."
version: "1.0.0"
---

# Conditionally render HTML in a C# component template

**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/render-conditional-content-in-template/render-conditional-content-in-template.zip)

Show a confirmation when `Saved` is true.

```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.

```html
<template if="{Saved}">
  <p>Changes saved.</p>
</template>
<template else>
  <p>Unsaved changes.</p>
</template>
```

Save the markup above as `SaveNotice.html`.

[The example](https://recursiveai.org/oneui/cookbook/render-conditional-content-in-template/render-conditional-content-in-template.zip) is the full program below: unzip it and `dotnet run`.

The template renders one paragraph: `Unsaved changes.` first and `Changes saved.` after setting `Saved`.

## The full program

Create a `dotnet new console` project and add the package as shown above, or use the download with all the files in place. The NuGet package includes the generator: it matches `SaveNotice.html` to `SaveNotice.cs` and implements the partial properties. The example’s project file pins the package with:

```xml
<PackageReference Include="RecursiveAI.OneUI" Version="1.0.0" />
```

`SaveNotice.cs`:

```csharp
using OneUI.Components;

public partial class SaveNotice
{
    [State]
    public partial bool Saved { get; set; }
}
```

Replace `Program.cs` with the code below. It mounts the component on a `ReferenceDocument`, an in-memory HTML tree, and prints the result without opening a window.

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

var dom = new ReferenceDocument();
var ui = new UiContext(dom);
dom.Context = ui;
var notice = new SaveNotice();
ui.Mount(notice);
Console.WriteLine(dom.ToHtml());
notice.Saved = true;
Console.WriteLine(dom.ToHtml());
```

`dotnet run` prints:

```text
<p>Unsaved changes.</p>
<p>Changes saved.</p>
```

If `else` doesn’t follow a sibling `if` or `else-if`, the compiler reports `OUI0050`. See [markup diagnostics](https://recursiveai.org/oneui/docs/diagnostics/).

## Variations

**Render only the confirmation.** When there’s nothing to show before saving, replace `SaveNotice.html` with just the `if` branch:

```html
<template if="{Saved}">
  <p>Changes saved.</p>
</template>
```

The first HTML snapshot is empty; the second contains the confirmation paragraph.

## Related recipes

- [Build a reactive HTML component with C# state](https://recursiveai.org/oneui/cookbook/create-html-component-with-csharp-state/)
- [Two-way bind an HTML input to C# state](https://recursiveai.org/oneui/cookbook/two-way-bind-input-value/)

## The short answer

Put `<template if="{Saved}">` around the elements you want when the condition is true. Add a sibling `<template else>` for the fallback. `Saved` is a Boolean `[State]` property on the component, so changing it switches branches.

## How do you write if and else in an HTML template?

Attach `if` to a `<template>` wrapper and put the C# Boolean expression in braces. The wrapper groups a branch; it doesn’t become a rendered HTML element. Put `else` immediately after the `if` template at the same nesting level. Don’t put it inside the `if` body or insert a heading between the two branches. Both branches can contain more than one element.

If you have three states, such as saving, saved and unsaved, use an `else-if` sibling between `if` and `else`. Order the conditions so the one you want to take precedence comes first. If more than one is true, only the first matching branch is rendered. Omit `else` when nothing should be rendered if all conditions are false.

## What’s the difference between conditional rendering and hiding an element?

Conditional rendering controls whether a branch exists in the document at all. In the example, the unsaved paragraph is removed when the saved paragraph is inserted. That’s useful when a UI should contain exactly one status message, or when controls in an inactive branch shouldn’t exist. If the branch contains a child component, switching away removes it; switching back creates a new instance, not a return to the old child’s local state.

If you need to keep an existing element and change only its appearance, bind a class or an inline style on that element instead. And if only the message text needs to change, a text binding can update one existing paragraph without swapping the paragraph itself. Use the branch syntax when the elements, child components, or structure differ between cases.
