Ninjasoft.HtmlBuilder 1.1.0

dotnet add package Ninjasoft.HtmlBuilder --version 1.1.0
NuGet\Install-Package Ninjasoft.HtmlBuilder -Version 1.1.0
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="Ninjasoft.HtmlBuilder" Version="1.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Ninjasoft.HtmlBuilder --version 1.1.0
#r "nuget: Ninjasoft.HtmlBuilder, 1.1.0"
#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.
// Install Ninjasoft.HtmlBuilder as a Cake Addin
#addin nuget:?package=Ninjasoft.HtmlBuilder&version=1.1.0

// Install Ninjasoft.HtmlBuilder as a Cake Tool
#tool nuget:?package=Ninjasoft.HtmlBuilder&version=1.1.0

Ninjasoft.HtmlBuilder

Purpose

The purpose of this project is to provide a utility to build webpages (or any HTML content) in code.

Usage

Start by creating a new instance of HtmlDocument in the following way

var document = new HtmlDocument();

This is setup as a builder pattern so the last method that should be called is Build which returns the entire HTML string.

From HtmlDocument there are a number of methods available: AddBody, AddHead, SetAttribute, SetClass. The last two methods are available on almost every subsequent builder. As mentioned previously, this uses the builder pattern to create HTML documents. Most of the builders have subsequent builders that can be utilized to build more complex HTML.

Example

Below is an example of a fairly simple webpage.

var html = new HtmlDocument()
    .AddHead(head => 
        head.AddMeta(meta => meta.Set("name", "viewport").Set("content", "width=device-width, initial-scale=1.0"))
            .AddMeta(meta => meta.Set("charset", "utf-8"))
            .SetTitle("HTML Builder")
            .AddLink(link => link.SetRel("stylesheet").SetType("text/css").SetHref("css/lib/bootstrap-5.1.3.min.css")))
    .AddBody(body => 
        body.AddDiv(div => div.SetClass("container")
                .AddHeading(Heading.H1, "HTML Builder")
                .AddForm(form => form.SetId("form1")
                    .AddDiv(div => div.SetClass("mb-3")
                        .AddLabel("Name")
                        .AddInput(InputType.Text, input => input.SetClass("form-control"))
                    )
                    .AddDiv(div => div.SetClass("mb-3")
                        .AddLabel("Quantity")
                        .AddSelect(select => select.SetClass("form-control")
                            .AddOption("Option 1")
                            .AddOption("Option 2"))
                        )
                    .AddDiv(div => div.SetClass("mb-3")
                        .AddLabel("Notes")
                        .AddTextarea(textarea => textarea.SetClass("form-control")))
                )
                .AddButton(button => button.SetType(ButtonType.Button).SetClass("btn btn-primary").SetText("Load"))
                .AddHorizontalRule()
                .AddTable(table =>
                    table.SetClass("table")
                    .AddHead(thead => thead.AddRow(tr => tr.AddDataHeading("Name").AddDataHeading("Address")))
                    .AddBody(tbody => tbody.AddRow(tr => tr.AddData("John Doe").AddData("123 Fake St")))
                )
                .AddHorizontalRule()
                .AddParagraph("")
                .AddLineBreak()
                .AddAnchor(a => a.SetValue("Click Here").SetHref("#"))
                .AddLineBreak()
                .AddImage("images/transparent-ninjasoft-nuget.png")
                .AddList(ListType.UnorderedList, ul => 
                    ul.Add("Item 1")
                        .Add("Item 2")
                        .Add("Item 3"))
            )
            .AddScript(ScriptType.JavaScript, "js/lib/jquery-3.6.0.min.js")
            .AddScript(ScriptType.JavaScript, "js/lib/bootstrap.bundle-5.1.3.min.js")
        )
    .Build();
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net6.0

    • No dependencies.

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.1.0 232 10/16/2023
1.0.1 93 9/21/2023
1.0.0 88 9/21/2023

Added builders for button, div, form, input, textarea. Added ability to set attributes on most elements.