Fuzzy.Blazor.FluentRenderTreeBuilder 2.0.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Fuzzy.Blazor.FluentRenderTreeBuilder --version 2.0.0                
NuGet\Install-Package Fuzzy.Blazor.FluentRenderTreeBuilder -Version 2.0.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Fuzzy.Blazor.FluentRenderTreeBuilder" Version="2.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Fuzzy.Blazor.FluentRenderTreeBuilder --version 2.0.0                
#r "nuget: Fuzzy.Blazor.FluentRenderTreeBuilder, 2.0.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Fuzzy.Blazor.FluentRenderTreeBuilder as a Cake Addin
#addin nuget:?package=Fuzzy.Blazor.FluentRenderTreeBuilder&version=2.0.0

// Install Fuzzy.Blazor.FluentRenderTreeBuilder as a Cake Tool
#tool nuget:?package=Fuzzy.Blazor.FluentRenderTreeBuilder&version=2.0.0                

FluentRenderTreeBuilder

The FluentRenderTreeBuilder library extends Blazor's built-in RenderTreeBuilder with a clean and fluent API for building Blazor components in pure C# code, and automatically generates source code line-based sequence numbers.

The resulting markup code is identical to the razor compiler output, with minor whitespace differences, and optionally output can be automatically minified by disabling the built-in 'pretty printing' feature, which is enabled by default to match the behaviour of Blazor's razor page compiler.

Fluent API

Developers don't have to hand-write repetitive calls to the built-in RenderTreeBuilder as the FluentRenderTreeBuilder allows fluent chaining of calls.

Taking an example from Chris Sainty's blog, this code:

protected override void BuildRenderTree(RenderTreeBuilder builder)
{
  base.BuildRenderTree(builder);
  builder.OpenElement(0, "nav");
  builder.AddAttribute(1, "class", "menu");
  
  builder.OpenElement(2, "ul");
  builder.OpenElement(3, "li");
  builder.OpenComponent<NavLink>(4);
  builder.AddAttribute(5, "href", "/");
  builder.AddAttribute(6, "Match", NavLinkMatch.All);
  builder.AddAttribute(7, "ChildContent", (RenderFragment)((builder2) => {
    builder2.AddContent(8, "Home");
  }));
  builder.CloseComponent();
  builder.CloseElement();
  
  builder.OpenElement(9, "li");
  builder.OpenComponent<NavLink>(10);
  builder.AddAttribute(11, "href", "/contact");
  builder.AddAttribute(12, "ChildContent", (RenderFragment)((builder2) => {
    builder2.AddContent(13, "Contact");
  }));
  builder.CloseComponent();
  builder.CloseElement();
  builder.CloseElement();
  builder.CloseElement();
}

can now be written as:

protected override void BuildRenderTree(RenderTreeBuilder builder)
  => builder.Build()
    .OpenElement("nav", "menu")
      .OpenAutoList()
        .NavLink("/", "Home", NavLinkMatch.All)
      .NewItem()
        .NavLink("/contact", "Contact")
    .CloseAll();

There are many new convenience methods and parameters provided, such as automatically generating id and class attributes from optional parameters to OpenElement, as shown above with the menu CSS class.

See the pages and components in the test app provided in the source code repo on GitHub for more usage examples.

Extensibility

Extension methods can be used to add new high-level functionality, and many are already included to help with table, list and attribute generation. In the above code snippet the OpenElement method which takes optional CSS class and id attributes extends the built-in OpenElement method.

For example, an extension method to automatically generate NavLink content might look like this:

public static FluentRenderTreeBuilder NavLink(this FluentRenderTreeBuilder fluentbuilder,
    string url, string markup, NavLinkMatch match = NavLinkMatch.All,
    string? @class = null, string? id = null)
  => fluentbuilder
    .OpenComponent<NavLink>(@class ?? "nav-link", id)
      .Attribute("href", url)
      .Attribute("Match", match)
      .ChildContent((MarkupString) markup, prettyPrint: true)
      .NewLine(prettyPrint: true)
    .Close();

This would then allow everything from the OpenComponent<NavLink>() call to its matching Close() call in the above code snippet to be replaced by a single call to NavLink("/", "Home"), making navigation list generation possible with just a few lines of code.

Further, using the extensibility example Menu() extension method (included in the test app in the source repo), it can be reduced even further to:

protected override void BuildRenderTree(RenderTreeBuilder builder)
  => builder.Build()
    .Menu(
      ("", "home", "Home"),
      ("contacts", "people", "Counter"),

The final example above also includes an icon parameter for each menu item which sets the nav item's icon.

Source code line-based sequence numbers

When using FluentRenderTreeBuilder developers don't have to manually provide and maintain the sequence numbers for each call to RenderTreeBuilder.

Unlike variable-based or calculated sequence numbers, which must not be used in Blazor components, FluentRenderTreeBuilder makes use of the [CallerLineNumber] attribute to generate each tree frame's sequence number based on the calling source code line number.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (4)

Showing the top 4 NuGet packages that depend on Fuzzy.Blazor.FluentRenderTreeBuilder:

Package Downloads
KingTech.Web.FormGenerator

Package Description

KingTech.Web.Markdown

Package Description

KingTech.Web.Markdown.NuGet

Package Description

Markdown2Markup

Blazor markdown to HTML converter based on MarkDig.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.1 2,821 6/30/2021
2.2.0 979 8/24/2020
2.1.0 959 8/17/2020
2.0.0 952 8/9/2020

Version history:

2.0.0
- Added `key` parameter on various methods for automatic generation of `SetKey()` calls.
- Added `Break()` to generate `<br />`.
- Added `Close(n)` to close multiple nested blocks.
- Added `CloseAll()` to close all open blocks.
- Added `CloseHelper()` to allow methods generating composite content to handle their own block closing.
- BREAKING CHANGE: removed `CloseAutoTable`, `CloseTableBody`, `CloseAutoList` (now replaced with `CloseHelper`s).
- Improved example code.
- Improved logging output.
- Updated documentation.

1.1.1
- Updated readme and release notes.

1.1.0
- Added full code documentation.
- Added list extension methods for list generation.
- Added more example code.

1.0.1
- Removed an unnecessary dependency.

1.0.0
- Initial release.


Thanks for checking it out!
Fuzzy Work Ltd.