FluentHtml (Core)
The core FluentHtml package provides the fundamental building blocks for HTML generation.
Installation
dotnet add package FluentHtml.Core
Key Types
Node
Abstract base class for all nodes in the component tree.
Children- List of child nodesAddChild(Node)- Add a child nodeAddChildren(IEnumerable<Node>)- Add multiple child nodesAddText(string)- Add a text content child
Element
Abstract base class for all HTML elements.
Fluent Methods:
Id(string)- Set the id attributeClass(string)- Add CSS classesStyle(string)- Set inline stylesTitle(string)- Set the title attributeData(string, string)- Set data attributesAria(string, string)- Set ARIA attributesRole(string)- Set the role attributeTabIndex(int)- Set tab orderHidden()- Mark as hiddenDisabled()- Mark as disabledRequired()- Mark as requiredReadOnly()- Mark as read-onlyChecked()- Mark as checkedCustom(string, string)- Set custom attributesOn(string, string)- Set event handlers
Component
Abstract base class for reusable UI components.
public abstract class Component : Node
{
public abstract Node Render();
}
public abstract class Component<T> : Component
{
protected T Data { get; }
protected abstract Node Build(T data);
}
Factory Methods
All HTML elements have factory extension methods:
Div() // Creates <div></div>
Span("text") // Creates <span>text</span>
Button("Click") // Creates <button>Click</button>
Input() // Creates <input />
Form(children) // Creates <form>...</form>
Rendering
Convert a node tree to HTML using the Renderer (using FluentHtml.Rendering;):
var renderer = new Renderer();
string html = renderer.Render(node);
// or write to a TextWriter:
renderer.Render(node, textWriter);
HtmlWriter
Low-level HTML output builder.
| Method | Description |
|---|---|
WriteRaw(string) |
Write raw HTML |
WriteOpenTag(string) |
Write opening tag |
WriteCloseTag(string) |
Write closing tag |
WriteSelfClosingTag(string) |
Write self-closing tag |
WriteAttribute(string, string) |
Write attribute with value |
WriteBooleanAttribute(string) |
Write boolean attribute |
WriteText(string) |
Write encoded text |
ToStringAndClear() |
Get output and clear buffer |
HtmlEncoder
HTML encoding utilities.
| Method | Description |
|---|---|
Encode(string) |
Encode string for safe HTML output |
Encode(string, StringBuilder) |
Encode and append to builder |