Bliss.Html.Core 0.4.0

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

Bliss.Html.Core

This library provides a simple abstraction for authoring reusable HTML components in C#.

Example

The following example demonstrates how to create a simple button component by inheriting from the HtmlElement class.

using Bliss.Html.Core;

public class BootstrapBtn : HtmlElement
{
    public BootstrapBtn(string text) : base("button", kids: new Kids { text })
    {
        Attrs.Add("class", "btn btn-primary");
    }
}

// we can create a new instance of our button component
var btn = new BootstrapBtn("Click me");

// when rendered, this will produce the following HTML string: 
// => <button class="btn btn-primary">Click me</button>

This is a slightly more complex example that demonstrates how to create a simple card component.

public class BootstrapCard : HtmlElement
{
    public BootstrapCard(string title, string body) : base("div")
    {
        Kids.Add(new HtmlElement("h5", 
            kids: new Kids { title }));
        Kids.Add(new HtmlElement("p", 
            kids: new Kids { body }));
        
        Attrs.Add("class", "card");
    }
}

// we can create a new instance of our card component
var card = new BootstrapCard("Card title", "Some quick example text to build on the card title and make up the bulk of the card's content.");

// when rendered, this will produce the following HTML string:
// => <div class="card">
//      <h5>Card title</h5>
//      <p>Some quick example text to build on the card title and make up the bulk of the card's content.</p>
//    </div>

You can also use the HtmlElement class directly for "one-off" components, like so:

var card = new HtmlElement("div", 
    attrs: new Attrs 
    { 
        { "class", "card" } 
    },
    kids: new Kids
    {
        new HtmlElement("h5", kids: new Kids { "Card title" }),
        new HtmlElement("p", kids: new Kids { "Some quick example text to build on the card title and make up the bulk of the card's content." })
    });

Rendering

Any IElement instance can be rendered to a string using the built-in BlissStringWriter class, which provides an implementation of the IWriter<string> interface:

var heading = new HtmlElement("h1", kids: new Kids { "Hello, world!" });

var writer = new BlissStringWriter();
card.WriteTo(writer);
var html = writer.ToString();

// the value of `html` will be:
// => <h1>Hello, world!</h1>

This method will be most useful when rendering a component for an email template, or for an HTML report, but this library provides most value when integrated with it's sister library, Bliss.Html.AspNetCore, which provides various integrations for working with ASP.NET Core (MVC, Razor Pages, etc.).

For example, instances of IElement can be returned directly from an ASP.NET Core controller action, by returning an instance of BlissResult:


using Bliss.Html.AspNetCore;

[Route("/")]
public class HomeController : Controller
{
    [HttpGet]
    public IActionResult Index()
    {
        var heading = new HtmlElement("h1", kids: new Kids { "Hello, world!" });
        return new BlissResult(heading);
    }
}

This will render the IElement as an HTML string to the response body, and set the Content-Type header to text/html.

Bliss.Html.AspNetCore also provides an extension method on the IHtmlHelper interface called Zap(IElement), which allows consumers to inject IElement instances directly into their Razor views:


@using Bliss.Html.AspNetCore

<h1>Hello, world!</h1>
@await Html.Zap(new BootstrapBtn("Click Me"))

Installation

This library is available as a NuGet package, which can be installed using the following command:

dotnet add package Bliss.Html.Core
Product 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.1

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Bliss.Html.Core:

Package Downloads
Bliss.Html.AspNetCore

A .NET library for authoring reusable HTML components in C# with ASP.NET Core.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
0.4.0 216 2/29/2024
0.3.0 178 2/29/2024
0.2.0 253 2/27/2024
0.1.0 194 2/27/2024