Expand description
§html-builder
A minimal, zero-dependency, no-std compatible HTML builder for Rust.
This crate provides two APIs:
- Untyped API: Dynamic HTML construction with runtime tag names
- Typed API (via
typedmodule): Compile-time validated HTML structure
§Typed API (Recommended)
The typed API uses Rust’s type system to validate HTML structure at compile time:
use html_builder::typed::{Document, Element};
use html_elements::{Html, Head, Body, Title, H1, Div, P, Meta};
let page = Document::new()
.doctype()
.root::<Html, _>(|html| {
html.attr("lang", "en")
.child::<Head, _>(|head| {
head.child::<Meta, _>(|m| m.attr("charset", "UTF-8"))
.child::<Title, _>(|t| t.text("My Page"))
})
.child::<Body, _>(|body| {
body.child::<H1, _>(|h| h.text("Welcome"))
.child::<P, _>(|p| p.text("Hello, World!"))
})
})
.build();Invalid nesting (e.g., <ul><div>) produces a compile-time error.
§Untyped API
The untyped API allows dynamic HTML construction:
use html_builder::{Html, Node};
let html = Html::new()
.elem("table", |e| e
.attr("class", "table table-sm")
.child("thead", |e| e
.child("tr", |e| e
.child("th", |e| e.text("Index"))
.child("th", |e| e.text("Address"))
)
)
.child("tbody", |e| e
.child("tr", |e| e
.child("td", |e| e.text("0"))
.child("td", |e| e
.child("code", |e| e.text("t1abc...xyz"))
)
)
)
)
.build();Modules§
- typed
- Typed HTML Builder
Structs§
- Element
- An HTML element with tag, attributes, and children.
- Html
- HTML builder for constructing HTML documents.
Enums§
- Node
- A node in the HTML tree - either an element or text.
Functions§
- div
- Create a div element.
- escape_
attr - Escape special characters in attribute values.
- escape_
html - Escape special HTML characters in text content.
- span
- Create a span element.
- table
- Create a table element.
- text_
elem - Create a simple text element.