HtmlAsCode 0.0.7
dotnet add package HtmlAsCode --version 0.0.7
NuGet\Install-Package HtmlAsCode -Version 0.0.7
<PackageReference Include="HtmlAsCode" Version="0.0.7" />
<PackageVersion Include="HtmlAsCode" Version="0.0.7" />
<PackageReference Include="HtmlAsCode" />
paket add HtmlAsCode --version 0.0.7
#r "nuget: HtmlAsCode, 0.0.7"
#:package HtmlAsCode@0.0.7
#addin nuget:?package=HtmlAsCode&version=0.0.7
#tool nuget:?package=HtmlAsCode&version=0.0.7
HTML as code
A HTML generation library based on the belief that components written in a general purpose language are better than templates.
Usage
See the tests.
Some examples:
var html = H("html",
H("head", H("title", "Hello World!"),
H("body",
H("h1", "Hello World!"),
H("p", "This is a paragraph."));
Console.WriteLine(html.RenderPretty(maxColumn: 40));
This will create the following HTML (notice the maxColumn parameter which is set to a rather low value to trigger this particular formatting - a larger maxColumn value will result in output that has longer, fewer lines):
<!DOCTYPE html>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<p>This is a paragraph.</p>
</body>
</html>
How to use components?
Just write a function that returns the body element:
Element MakeBody() => H("body", H("h1", "Hello World!"), H("p", "This is a paragraph."));
(notice the return type of MakeBody, this is the Element provided by the this library)
Then you can call this function in your HTML generation code:
var html = H("html",
H("head", H("title", "Hello World!")),
MakeBody());
This is where the "as code" part starts to matter. We can parametrize the function acting as components.
Element MakeBody(string title, string content) => H("body", H("h1", title), H("p", content));
Using this component gives us:
var html = H("html",
H("head", H("title", "Hello World!")),
MakeBody("Hello World!", "This is a paragraph."));
You may have noticed that text nodes are specified as strings. These strings are escaped by the library.
To insert verbatim HTML, use the R function:
var html = H("html",
H("head", H("title", "Hello World!")),
H("body", H("h1", "Hello World!"), R("<p>This is a paragraph.</p>")));
If you need to flatten lists of elements, use the F function:
var html = H("html",
H("head", H("title", "Hello World!")),
H("body",
H("h1", "Hello World!"),
F(H("p", "This is a paragraph."), H("p", "Another paragraph."))));
... or you could just use a component that returns a IEnumerable<Element> instead of Element.
F is just a helper function to quickly create a list of elements.
| Product | Versions 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. 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. |
-
net8.0
- FancyPen (>= 0.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.