| Crates.io | hatmil |
| lib.rs | hatmil |
| version | 1.0.2 |
| created_at | 2025-04-04 15:31:35.870722+00 |
| updated_at | 2026-01-20 16:47:08.649063+00 |
| description | Simple HTML/SVG builder |
| homepage | |
| repository | https://github.com/DougLau/hatmil |
| max_upload_size | |
| id | 1620223 |
| size | 139,349 |
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>"
);
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 attributecite_el on BlockQuote and Q, clash with cite attributeform_el on FieldSet, clash with form attributeslot_el on many elements, clash with slot global attributestyle_el on Head, NoScript and SVG elements, clash with style
global attributetitle_el on Head and SVG Style, clash with title global attributeSome HTML names clash with Rust keywords. In these cases, raw identifiers must be used to call those methods: