Crate ironhtml

Crate ironhtml 

Source
Expand description

§ironhtml

A minimal, zero-dependency, no_std compatible HTML builder for Rust.

The [html!] macro is the easiest way to build type-safe HTML. Enable it with features = ["macros"]:

use ironhtml::html;

let page = html! {
    html.lang("en") {
        head {
            meta.charset("UTF-8")
            title { "My Page" }
        }
        body {
            h1 { "Welcome" }
            p { "Hello, World!" }
        }
    }
};

assert!(page.render().contains("<h1>Welcome</h1>"));

The macro supports attributes, Rust expressions, loops, and conditionals:

use ironhtml::html;

let items = vec!["Apple", "Banana", "Cherry"];
let show_title = true;

let nav = html! {
    div.class("container").id("main") {
        if #show_title {
            h1.class("title") { "Fruit List" }
        }
        ul.class("list") {
            for item in #items {
                li { #item }
            }
        }
    }
};

let html = nav.render();
assert!(html.contains("<li>Apple</li>"));
assert!(html.contains("Fruit List"));

§Typed API

The typed API uses Rust’s type system to validate HTML structure at compile time. Invalid nesting (e.g., <ul><div>) produces a compile-time error. The html! macro generates typed API calls under the hood.

use ironhtml::typed::{Document, Element};
use ironhtml_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();

§Untyped API

The untyped API allows fully dynamic HTML construction with runtime tag names — useful when the structure is not known at compile time:

use ironhtml::{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.