Expand description
Hatmil is an HTML builder for Rust. It can be used to create or modify web pages dynamically, including inline SVG.
With a Tree, there are two “root” methods:
- html for a full document, including
DOCTYPEdeclaration - root for a snippet, starting from any root element (HTML or SVG)
In either case, an element struct is returned which borrows from the Tree.
Each element has methods for setting valid attributes, such as id. There
are also methods for adding permitted child elements.
use hatmil::Tree;
let mut tree = Tree::new();
let mut html = tree.html();
let mut body = html.body();
body.p().id("para").cdata("Graph");
assert_eq!(
String::from(tree),
"<!DOCTYPE html><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 tree, use String::from(tree) 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::{Tree, html::Div};
let mut tree = Tree::new();
let mut div = tree.root::<Div>();
div.button().class("rounded").cdata("Press Me!");
assert_eq!(
String::from(tree),
"<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:
Modules§
Structs§
- Path
DefBuilder - SVG Path definition builder
- Poly
Point Builder - SVG Polygon / Polyline point builder
- Tree
- HTML tree builder
- Value
- A value of an attribute or text content
Type Aliases§
- Page
Deprecated - Renamed to
Tree; will be removed in a future release