25 releases (4 stable)
Uses new Rust 2024
| 1.1.0 | Feb 2, 2026 |
|---|---|
| 0.15.0 | Jan 17, 2026 |
| 0.11.0 | Dec 28, 2025 |
| 0.10.0 | Nov 29, 2025 |
| 0.3.0 | Apr 6, 2025 |
#280 in Web programming
Used in splot
130KB
3.5K
SLoC
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:
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_elon Th, clash withabbrattributecite_elon BlockQuote and Q, clash withciteattributeform_elon FieldSet, clash withformattributeslot_elon many elements, clash withslotglobal attributestyle_elon Head, NoScript and SVG elements, clash withstyleglobal attributetitle_elon Head and SVG Style, clash withtitleglobal attribute
Some HTML names clash with Rust keywords. In these cases, raw identifiers must be used to call those methods: