Apply an edited template to a mounted component with TemplateReload.Apply. If the edit is rejected, print result.Diagnostics.
dotnet add package RecursiveAI.OneUIRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
// note: mounted component; edited: new HTML. var result = TemplateReload.Apply( note, edited, "StatusNote.html"); if (result.Status == TemplateReloadStatus.Rejected) { foreach (var d in result.Diagnostics) Console.WriteLine(d); }
The example is the full program below: unzip it and dotnet run.
This recipe checks edits applied to a running app. In a normal dotnet build, template errors appear in the build output as OUI diagnostics.
For style="colour: red", the printed diagnostic includes Id = OUI0101, File = StatusNote.html, Line = 1, and Column = 11. The full program formats those fields on one line.
The full program
Save the valid template as StatusNote.html next to Example.cs. OneUI’s source generator pairs the template with the partial StatusNote class. The zip uses an ordinary PackageReference to RecursiveAI.OneUI 1.2.1. Run it in Debug (the default) so OneUI includes its template reload data. The sample mounts the original in a ReferenceDocument (an in-memory document for testing without a window), then passes the invalid edit as a string. Don’t put the invalid edit on disk in place of the valid template before building this example.
<p>Ready</p>using OneUI.Components; using OneUI.Components.Reference; using OneUI.HotReload; var doc = new ReferenceDocument(); var ui = new UiContext(doc); var note = new StatusNote(); ui.Mount(note); const string edited = "<p style=\"colour: red\">Ready</p>"; var result = TemplateReload.Apply(note, edited, "StatusNote.html"); Console.WriteLine(result.Status); foreach (var d in result.Diagnostics) { Console.WriteLine($"{d.File}({d.Line},{d.Column}): {d.Id}: {d.Message}"); } Console.WriteLine($"Still rendered: {doc.Text("p")}"); public partial class StatusNote;
Rejected StatusNote.html(1,11): OUI0101: CSS property 'colour' is not supported by the renderer; did you mean 'color'? Still rendered: Ready
On Rejected, the mounted component stays as it was; use the reported position in the edited text to find the error.
Variations
To apply the correction, change the edited string in Example.cs as follows. The sample prints Applied and no diagnostic lines.
const string edited = "<p style=\"color: red\">Ready</p>";Related recipes
The short answer
Call TemplateReload.Apply with the mounted component, the edited HTML, and the file path you want in the report. When Status is Rejected, print each entry in Diagnostics. The Id identifies the compiler check; File, Line, and Column locate it in the edit; Message explains what to fix. Call on the UI thread in a build with template reload enabled.
How do you find the file, line, and column of a template error?
Read the location from the diagnostic itself, rather than looking for the line of the C# call to Apply. In StatusNote.html(1,11): OUI0101, the line and column are both one-based, and column 11 is where colour begins. If your editor needs a range, the diagnostic also has EndLine and EndColumn, with the end column just past the error. The optional sourcePath parameter labels the location; Apply reads the template text you pass it, not that path. When you leave out the path, OneUI uses the path recorded at build time when available.
How do you fix a compilation error in an edited template?
Start with the message. For OUI0101, check the CSS property name and the supported properties. Correcting colour to color makes this edit valid. For other IDs, use the diagnostic reference to tell an unrecognized attribute or event from a malformed tag. Reapply the corrected edit and check the status again.
If the status is NeedsRebuild instead of Rejected, read Causes: the running build is missing something the edit now uses, such as a new C# expression. Rebuilding and restarting supplies it. Unavailable is different again: check the result’s message to see whether reload support is off or the component isn’t mounted. Neither status means there’s a template compiler error at the reported line.