CC.CSX 0.1.31

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

// Install CC.CSX as a Cake Tool
#tool nuget:?package=CC.CSX&version=0.1.31                

CC.CSX

build

About

CC.CSX provides the ability to define and generate HTML output in a declarative fashion by just using pure C# or F# or other .Net based language.

The idea is to define strongly typed readable and ergonomic structures of HTML components, elements and attributes in a way that is similar to the way you would write HTML, but in a more structured and type-safe way.

This way the developer is able to easily organize, navigate and manipulate the final output.

It is similar like JSX in the JS world, or even more similar to hiccup in clojure.

How to use it

Main usage would be as Html Renderer, you can build entire pages, components and applications with it wihout the need to write any HTML(or JS).

For this you also need to install the CC.CSX.Web package from Nuget in order to have the Render method available.

You may also need the CC.CSX.Htmx package which provides the Htmx related attributes. This way you can build reactive applications with ease and without the need to write any JS or HTML.

Bellow you can find a complete version of the legendary Counter example, but this time in C#, Asp.Net using , Dotnet Minimal APIs and this library CC.CSX.

var builder = WebApplication.CreateBuilder(args); //classic bootstrapping
var app = builder.Build(); // create the app

int counter = 0;
app.MapGet("/", () => Render( // Render the Html node
    Master("Counter", // Master layout is just a method defined bellow in the code
        //we use hxPost to send a post request and update the counter with the new value
        Button("-", hxPost("/decrement", target: "#counter")), 
        Label(id("counter"), counter),
        Button("+", hxPost("/increment", target: "#counter"))
    )
));

// These are the post routes that will be called when the buttons 
// are clicked and will just return the new counter value
app.MapPost("/increment", () => Render(++counter));
app.MapPost("/decrement", () => Render(--counter));
app.Run();

// This method provides the master layout for the page and can be reused
static HtmlNode Master(string title, params HtmlNode[] content) 
=> Html(
    Meta(charset("utf-8")),
    Head(
        Title("Htnet Demo"),
        Meta(charset("utf-8")),
        HtmxImports),
    Body(
        H1(@class("text-center"), title),
        content, // this is the content that will be rendered as a child of the body
        Hr()
    )
);

How it works

As you may have noticed, there is no type declaration anywhere, but that does not mean we are not using strong types. The strings, and tuples are being used in the example above, are converted to HtmlAttribute, and HtmlNode through implicit operators.

There are quite a few implicit operators that are used to convert the types into proper HtmlNode or HtmlAttribute instances. This is what makes the whole declarative structure possible.

Most of the Html elements and attributes can be created by their static method counterparts(Div(...), H1(...), style(...), id(...), etc.). methods that return HtmlNode or HtmlAttribute instances.

Every HTML node has its defined method with the same name as the Element

  • using satatic CC.CSX.HtmlElements imports all the methods that create HTML Nodes.
  • using satatic CC.CSX.HtmlAttributes imports all the methods that create HTML Attributes.

Some more notable implicit operators are:

  • Any parameter that is a tuple of two strings (key and the value) is converted to HtmlAttribute
  • Any string, int, float, double or bool parameter is converted to HtmlTextNode which is a special node that just contains then text representation of the value.
  • An array of HtmlNode is converted to HtmlFragment which is a special node that contains a list of nodes.
  • An array of HtmlAttribute is converted to MultiAttr which is a special attribute that contains a list of attributes.

Take a look at the following example:

Div((style, "background:silver;"), /* attribute of the div */
  "Hello HTML", /* text node*/
  H1("Hello world"), /* h1 node with text node inside */
  Article((id, "article-1"), 
    P("Some content here")
  )
)

This will generate the following HTML:

<div style="background:silver;">
  Hello HTML
  <h1>
    Hello world
  </h1>
  <article id="article-1">
    <p>
      Some content here
    </p>
  </article>
</div>

For existing HTML elements and attributes, you can use the static methods provided by the HtmlElements and HtmlAttributes classes, if you need to create custom elements you can use the new HtmlNode constructor, and tuple for attributes.

TODO

  • Project template

    • that scaffolds a new project with all the needed packages
  • WebAssembly support

  • Think of a better name

  • Performance optimizations

    • Use span instead of strings.
    • Compile a template and just compute the dynamic parts when needed.
  • Reactive features

    • Ability to process events directly from C# instead of using JS.
      • Server side and client side
      • Some form of state management
      • More native HTMX integration
  • Better documentation and examples

  • Native CSS support

    • Ability to define CSS in C# and have it compiled to CSS
    • Maybe port Tailwind CSS to C#
    • Do somethig like Stitches but in C#
  • Library for building mobile apps on top of MAUI

Public Code is here: https://github.com/codechem/cc.csx

There are three packages packaged in this repo:

  • CC.CSX providing the core functionality explained bellow in this document

  • CC.CSX.Web useful extensions for using the core package in ASP.NET Core

  • CC.CSX.Htmx collection of attribute methods for HTMX

Contributions and ideas are welcome.

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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • net8.0

    • No dependencies.

NuGet packages (2)

Showing the top 2 NuGet packages that depend on CC.CSX:

Package Downloads
CC.CSX.Web

CC.CSX.Web extends CC.CSX with methods for easy creation of Html responses in APIs

CC.CSX.Htmx

CC.CSX.Htmx extends CC.CSX with tags methods for Htmx

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.1.31 129 9/2/2024
0.1.22 108 9/2/2024
0.1.20 194 2/23/2024
0.1.19 220 1/8/2024
0.1.18 258 11/14/2023
0.1.17 214 10/2/2023
0.1.16 209 9/29/2023
0.1.15 178 9/29/2023
0.1.13 175 9/27/2023
0.1.11 171 9/22/2023
0.1.10 160 9/20/2023
0.1.8 165 9/17/2023
0.1.7 127 9/17/2023