Source Generator
The source generator is bundled with Tombatron.Turbo — no extra package needed. It scans _*.cshtml partial views at compile time and generates a Partials class (in Tombatron.Turbo.Generated) with strongly-typed references.
Usage
// Use generated references instead of string names:
await builder.AppendAsync("messages", Partials.Message, message);
How It Works
- At compile time, the generator finds all
_*.cshtmlfiles in your project - It generates a static
Partialsclass with aPartialTemplateproperty for each partial - These references can be used with
ITurboStreamBuilderandTurboResultsmethods
Benefits
- Compile-time safety — Typos in partial names are caught at build time, not runtime
- Refactoring support — Rename a partial and the compiler flags all usages
- IntelliSense — Autocomplete for partial names in your IDE
Example
Given a partial Pages/Shared/_CartItem.cshtml, the generator creates:
// Auto-generated
public static class Partials
{
public static PartialTemplate CartItem { get; }
}
Use it in stream builders:
await _turbo.Stream($"user:{userId}", async builder =>
{
await builder.AppendAsync("cart-items", Partials.CartItem, newItem);
});
Or in Minimal API endpoints:
return TurboResults.Partial(Partials.CartItem, item);