hatmil

Crates.iohatmil
lib.rshatmil
version1.0.2
created_at2025-04-04 15:31:35.870722+00
updated_at2026-01-20 16:47:08.649063+00
descriptionSimple HTML/SVG builder
homepage
repositoryhttps://github.com/DougLau/hatmil
max_upload_size
id1620223
size139,349
Douglas Lau (DougLau)

documentation

https://docs.rs/hatmil

README

Hatmil is an HTML builder for Rust. It can be used to create or modify web pages dynamically, including inline SVG.

With a Page, there are two "root" methods:

  • html for a full document
  • frag for a fragment, starting from an arbitrary element (HTML or SVG)

In either case, an element struct is returned which borrows from the Page.

Each element has methods for setting valid attributes, such as id. There are also methods for adding permitted child elements.

use hatmil::Page;

let mut page = Page::new();
let mut html = page.html();
let mut body = html.body();
body.p().id("para").cdata("Graph");
assert_eq!(
    String::from(page),
    "<html><body><p id=\"para\">Graph</p></body></html>"
);

Text content (character data) can be added using the cdata or cdata_len methods on an element. Special HTML characters will automatically be replaced by character references, as needed (for content which has already been escaped, use the raw method). The close method can be used to close the final open element.

After creating the page, use String::from(page) to get the resulting HTML. Any open tags will be closed automatically. Display is also implemented, enabling the use of format or to_string().

use hatmil::{Page, html::Div};

let mut page = Page::new();
let mut div = page.frag::<Div>();
div.button().class("rounded").cdata("Press Me!");
assert_eq!(
    String::from(page),
    "<div><button class=\"rounded\">Press Me!</button></div>"
);

Method Names

In most cases, element methods match the HTML tag exactly. But due to clashes with attribute names, some methods for creating child elements have an _el suffix:

  • abbr_el on Th, clash with abbr attribute
  • cite_el on BlockQuote and Q, clash with cite attribute
  • form_el on FieldSet, clash with form attribute
  • slot_el on many elements, clash with slot global attribute
  • style_el on Head, NoScript and SVG elements, clash with style global attribute
  • title_el on Head and SVG Style, clash with title global attribute

Some HTML names clash with Rust keywords. In these cases, raw identifiers must be used to call those methods:

  • r#as on Link

  • r#async on Script

  • r#for on Label and Output

  • r#in on multiple SVG filter elements

  • r#loop on Audio and Video

  • r#type on multiple HTML and SVG elements

  • r#use (SVG element)

Commit count: 153

cargo fmt