Recursive AI

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.

Show a confirmation when Saved is true.

Terminal
dotnet add package RecursiveAI.OneUI

Requires .NET 10 (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 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:

C#
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.

C#
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.

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.

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.

All oneui recipes