Skip to content

Instantly share code, notes, and snippets.

@dharmatech
Last active January 20, 2022 11:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dharmatech/a19ed9f129b8abfc468f7b0e1d588061 to your computer and use it in GitHub Desktop.
Save dharmatech/a19ed9f129b8abfc468f7b0e1d588061 to your computer and use it in GitHub Desktop.

HTML Generation Libaries for C#

Do you know of any others? Feel free to leave a comment below. Thanks!

Example:

new HtmlTag("html").Append(new[] { 

    new HtmlTag("head")
        .Append(new HtmlTag("title").Text("Abc")),

    new HtmlTag("body")
        .Append(new HtmlTag("h1").Text("People"))
        .Append(
            new HtmlTag("table")
                .Append(
                    new HtmlTag("thead").Append(new []{ 
                        new HtmlTag("th").Text("Name"),
                        new HtmlTag("th").Text("Birth date"),
                        new HtmlTag("th").Text("Height"),
                    }))
                .Append(
                    new HtmlTag("tbody").Append(People().Select(person => 
                        new HtmlTag("tr")
                            .Append(new HtmlTag("td").Text(person.Name))
                            .Append(new HtmlTag("td").Text(person.BirthDate))
                            .Append(new HtmlTag("td").Text(person.Height.ToString()))))))

        .Append(new HtmlTag("div").AddClass("abc").Text("xyz"))

        .Append(new HtmlTag("div").AddClass("a b c d").Text("bcd"))

        .Append(new HtmlTag("a").Attr("href", "https://www.gnu.org").Text("GNU"))

})
  • Developed by Ben Albahari, author of C# in a Nutshell
  • Very concise
  • Output is pretty printed
  • Not frequently updated (minor patch in 2019, last major update in 2014)

Example:

H.html(

    H.head(H.title("bcd")),

    H.body(

        H.h1("People"),

        H.table(
            H.thead(
                H.th("Name"),
                H.th("Birth date"),
                H.th("Height")),
            H.tbody(
                People().Select(person => H.tr(
                    H.td(person.Name),
                    H.td(person.BirthDate),
                    H.td(person.Height))))),

        H.div(a => a.Custom("class", "abc"), "xyz"),

        H.div(a => a.css("a b c d"), "bcd"),

        H.a(attrs => attrs.href("https://www.gnu.org"), "GNU"))
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment