HtmlAsCode 0.0.7

dotnet add package HtmlAsCode --version 0.0.7
                    
NuGet\Install-Package HtmlAsCode -Version 0.0.7
                    
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="HtmlAsCode" Version="0.0.7" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="HtmlAsCode" Version="0.0.7" />
                    
Directory.Packages.props
<PackageReference Include="HtmlAsCode" />
                    
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 HtmlAsCode --version 0.0.7
                    
#r "nuget: HtmlAsCode, 0.0.7"
                    
#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 HtmlAsCode@0.0.7
                    
#: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=HtmlAsCode&version=0.0.7
                    
Install as a Cake Addin
#tool nuget:?package=HtmlAsCode&version=0.0.7
                    
Install as a Cake Tool

HTML as code

A HTML generation library based on the belief that components written in a general purpose language are better than templates.

Usage

See the tests.

Some examples:

var html = H("html",
    H("head", H("title", "Hello World!"),
    H("body",
        H("h1", "Hello World!"),
        H("p", "This is a paragraph."));
Console.WriteLine(html.RenderPretty(maxColumn: 40));

This will create the following HTML (notice the maxColumn parameter which is set to a rather low value to trigger this particular formatting - a larger maxColumn value will result in output that has longer, fewer lines):

<!DOCTYPE html>
<html>
    <head>
        <title>Hello World!</title>
    </head>
    <body>
        <h1>Hello World!</h1>
        <p>This is a paragraph.</p>
    </body>
</html>

How to use components? Just write a function that returns the body element:

Element MakeBody() => H("body", H("h1", "Hello World!"), H("p", "This is a paragraph."));

(notice the return type of MakeBody, this is the Element provided by the this library)

Then you can call this function in your HTML generation code:

var html = H("html",
    H("head", H("title", "Hello World!")),
    MakeBody());

This is where the "as code" part starts to matter. We can parametrize the function acting as components.

Element MakeBody(string title, string content) => H("body", H("h1", title), H("p", content));

Using this component gives us:

var html = H("html",
    H("head", H("title", "Hello World!")),
    MakeBody("Hello World!", "This is a paragraph."));

You may have noticed that text nodes are specified as strings. These strings are escaped by the library.

To insert verbatim HTML, use the R function:

var html = H("html",
    H("head", H("title", "Hello World!")),
    H("body", H("h1", "Hello World!"), R("<p>This is a paragraph.</p>")));

If you need to flatten lists of elements, use the F function:

var html = H("html",
    H("head", H("title", "Hello World!")),
    H("body",
        H("h1", "Hello World!"),
        F(H("p", "This is a paragraph."), H("p", "Another paragraph."))));

... or you could just use a component that returns a IEnumerable<Element> instead of Element.

F is just a helper function to quickly create a list of elements.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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. 
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
0.0.7 378 2/6/2025
0.0.6 178 1/3/2025
0.0.5 154 1/3/2025
0.0.4 168 1/3/2025
0.0.3 175 1/3/2025
0.0.2 161 1/2/2025
0.0.1 134 1/2/2025