NgSharp 1.0.6

dotnet add package NgSharp --version 1.0.6
                    
NuGet\Install-Package NgSharp -Version 1.0.6
                    
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="NgSharp" Version="1.0.6" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="NgSharp" Version="1.0.6" />
                    
Directory.Packages.props
<PackageReference Include="NgSharp" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add NgSharp --version 1.0.6
                    
#r "nuget: NgSharp, 1.0.6"
                    
#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.
#:package NgSharp@1.0.6
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=NgSharp&version=1.0.6
                    
Install as a Cake Addin
#tool nuget:?package=NgSharp&version=1.0.6
                    
Install as a Cake Tool

NgSharp

NgSharp is a fast, modular, and extensible HTML rendering engine for .NET, built on top of AngleSharp and inspired by Angular’s philosophy.
It lets you generate HTML using templates with pipes, directives, and custom components, ideal for rendering PDFs, emails, or server-side HTML.

License: MIT NuGet


πŸš€ Features

  • βœ… Angular-style syntax (*if: [if], *for: [for], | pipe)
  • πŸ”§ Fully extensible: add your own pipes, directives, or components
  • ⚑ Blazing fast (~4–5 Β΅s per render)
  • 🧱 Supports custom components (e.g., static Google Maps)
  • πŸ§ͺ Snapshot-style HTML testing support
  • 🧩 No MVC or Razor dependency

🧩 Template syntax


<span>
    {{ MyDate | date: 'dd/MM/yyyy' }} 
</span>

<span>
    {{ MyNumber | number: 'N0' }} 
</span>

<span>
    {{ MyText | upper }}
</span>



<div [html]="MyHtmlContent"></div>

<a [attr.href]="MyLink"></a>

<span [attr.class]="MyAddedClass" class="initial__class"></span>

<span [style.color]="MySpanColor"></span>



<div [if]="User.IsActive">
    Welcome {{ User.Name | upper }}
</div>

<div [for]="Items">
    {{ Amount | largeNumber }}
</div>

<div [not-empty]="Items">
    <div [for]="Items">
        {{ Amount | largeNumber }}
    </div>
</div>


<map [points]="MapPoints" [Width]="MapWidth" [Height]="MapHeight"></map>

πŸ“Š Benchmarks

Engine Mean time Memory allocation
NgSharp ~4.6 Β΅s ~840 B
RazorLight ~61.4 Β΅s ~4.1 KB

NgSharp is up to 13Γ— faster and 5Γ— lighter than RazorLight in pure HTML rendering scenarios.


πŸ”Œ Custom Pipes

NgSharp allows you to build custom pipes with logic inside.

public class LowerCasePipe : IPipe
{
    public string PipeName => "lower";
    
    public string Transform(IElement childElement, NgElement value, string argument)
    {
        return value.GetString()?.ToLower();
    }
}

πŸ”Œ Custom Directives

NgSharp allows you to build custom directives with logic inside.

public class HiddenDirective : IDirective
{
    public string DirectiveName => "hidden";
    
    public bool ApplyWhileParsing => false;

    public void Apply(HtmlBuilder builder, string directiveName, IElement childElement, NgElement content, Dictionary<string, string> optionalArguments = null)
    {
        var booleanValue = content.GetBoolean();

        if (booleanValue == true)
        {
            childElement.SetAttribute("hidden", string.Empty);
        }
    }
}

πŸ”Œ Custom components

NgSharp allows you to build custom components with logic inside.
See the MapComponent for more informations

public class CustomComponent : IComponent
{
    public string ComponentName => "custom-component";
        
    public string ComponentText { get; set; }

    public void Render(IElement element)
    {
        var htmlParser = new HtmlParser();

        var image = 
            $"<div>" +
            $"{ComponentText}" +
            $"</div>";

        var node = htmlParser.ParseFragment(image, element);

        element.Parent.InsertBefore(node.First(), element);
        element.Parent.RemoveElement(element);
    }
}

πŸ› οΈ Installation

πŸ“¦ Coming soon to NuGet
(For now, clone this repository and reference NgSharp.csproj)


βš™οΈ Usage

var builder = HtmlBuilder.Default;
var templateModel = new
{
    FirstProperty = "MyFirstProperty"
};

var result = builder.Render(templateHtml, templateModel);

πŸ” RazorLight vs NgSharp

RazorLight NgSharp
Syntax Razor (.cshtml) HTML + Angular-style
Rendering mode Compiled (Roslyn) Interpreted parsing
Performance ⚠️ slow (uncached) ⚑ extremely fast
Custom logic ❌ difficult βœ… simple and modular
Component support ❌ no βœ… yes
Ideal for MVC views PDF, email, SSR API

πŸ“¦ Roadmap

  • Pipe + directive system
  • Custom component architecture
  • HTML-based snapshot tests
  • NuGet publication
  • Precompiled template support
  • Multi pipes parsing
  • Condition parsing improved
  • Custom template in html, like ng-template for angular

🀝 Contributing

Pull requests are welcome!
You can build and share your own pipes, directives or components too.


πŸ“„ License

MIT – free to use and modify.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.6 103 8/21/2025
1.0.5 96 8/21/2025
1.0.4 128 8/19/2025
1.0.3 121 8/19/2025
1.0.2 124 8/19/2025
1.0.1 53 8/1/2025