Crate html

source ·
Expand description

Typed HTML support for Rust.

HTML is not simple. There are many different nodes, attributes, relationships, and types - and no compiler to help with any of that This crate provides type-safe bindings to construct HTML elements natively in Rust. So you don’t have to remember whether <ol> takes a <li>, or whether it’s the other way around.

Examples

We can create HTML structures one-by-one:

use html::text_content::OrderedList;
let tree = OrderedList::builder()
    .list_item(|li| li.text("hello").class("pigeon").end())
    .list_item(|li| li.text("world").class("pigeon").end())
    .build();
let string = tree.to_string();

But we can also use Rust’s native control flow structures such as loops to iterate over items and create HTML:

use html::text_content::OrderedList;
let mut ol = OrderedList::builder();
for name in ["hello", "world"] {
    ol.list_item(|li| li.text(name).end());
}
let tree = ol.build();
assert_eq!(tree.to_string(), r#"<ol><li>hello</li><li>world</li></ol>"#);

Modules

Traits