HyperTextExpression

Same ol' C# with a different way to say what HTML you want.

using static HyperTextExpression.HtmlExp;

var html = HtmlDoc(
    ("body", Children(
        ("article",
            Attrs(
                ("class", "body"),
                ("show", "")
            ),
            Enumerable.Range(1, 6)
                .Select(i =>
                    ("p",
                        Attrs("class", "paragraph"),
                        $"Lorem Ipsum {i}")))))
);

html.ToString(); => <!DOCTYPE html>
                    <html>
                        <body>
                            <article class="body" show>
                                <p class="paragraph">Lorem Ipsum 1</p>
                                <p class="paragraph">Lorem Ipsum 2</p>
                                <p class="paragraph">Lorem Ipsum 3</p>
                                <p class="paragraph">Lorem Ipsum 4</p>
                                <p class="paragraph">Lorem Ipsum 5</p>
                                <p class="paragraph">Lorem Ipsum 6</p>
                            </article>
                        </body>
                    </html>

Within ASP.NET Core.

app.MapGet("/", () =>
    HtmlDoc(
        Body(
            ("h1", "Hello World")
        ))
        .ToIResult()
);

Motivation

  • Template straight in c#
  • No need to use/learn .cshtml Razor/View and then wait for things like email templating...
  • C# structure is MUCH easier to manipulate than an HTML string.
  • hiccup
  • tweet

Installation

Standalone

dotnet add package HyperTextExpression

AspNetCore Integration

dotnet add package HyperTextExpression.AspNetCore

Examples

Custom Serialization

  1. Implement IPrintHtml
public class MyPrintHtml : IPrintHtml
{
    public void Write(char c) => ...;
    public void Write(char c, int count) => ...;
    public void Write(ReadOnlySpan<char> chars) => ...;
}
  1. Dump HtmlEl to your implementation of IPrintHtml
var html = HtmlDoc(
        Body(
            ("h1", "Hello World")
        ));
        
var myPrint = new MyPrintHtml();
        
IPrintHtml.To(html, myPrint);