DSL for building HTML - inspired by nitter
Example
global using static HtmlTagHelpers.Prelude;
using HtmlTagHelpers;
var myElements = new[] { "a", "b", "c" };
var document = html()(
head()(
script()(
Raw(
"""
console.log('hello world')
"""
)
)
),
body()(
h1()("Title Page"),
p()("this is some content"),
// conditional expressions
If(true, span()("true"), span()("false")),
p(
("style", "border: 1px solid black;"),
("onClick", "console.log('clicked')")
)("this paragraph has a border"),
p(Attr("hidden"))("hidden content"),
MButton(("onClick", "console.log('override')"))("test"),
ul()(myElements.Select(i => li()(i)).ToArray())
)
);
// example functional component
TagBuilder MButton(params Eighty.Attr[] content) =>
button(content.Append(("onClick", "console.log('default')")).ToArray());
Console.WriteLine(document.ToString());
Outputs:
<html><head><script>console.log('hello world')</script></head><body><h1>Title Page</h1><p>this is some content</p><span>true</span><p style="border: 1px solid black;" onClick="console.log('clicked')">this paragraph has a border</p><p hidden>hidden content</p><button onClick="console.log('override')" onClick="console.log('default')">test</button><ul><li>a</li><li>b</li><li>c</li></ul></body></html>
- Add h1 - h6 missing tag helpers
- Add OneOf generation to create typesafe methods
- use eighty as a base - change to partial function application as the interface
dotnet run --project .\generator\ > .\src\HtmlTagHelpers.cs && dotnet csharpier .