NgSharp 1.0.6
dotnet add package NgSharp --version 1.0.6
NuGet\Install-Package NgSharp -Version 1.0.6
<PackageReference Include="NgSharp" Version="1.0.6" />
<PackageVersion Include="NgSharp" Version="1.0.6" />
<PackageReference Include="NgSharp" />
paket add NgSharp --version 1.0.6
#r "nuget: NgSharp, 1.0.6"
#:package NgSharp@1.0.6
#addin nuget:?package=NgSharp&version=1.0.6
#tool nuget:?package=NgSharp&version=1.0.6
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.
π 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 | Versions 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. |
-
.NETStandard 2.1
- AngleSharp (>= 1.3.0)
- SkiaSharp (>= 3.119.0)
- System.Text.Json (>= 9.0.7)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.