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 Tree;
let mut tree = new;
let mut html = tree.html;
let mut body = html.body;
body.p.id.cdata;
assert_eq!;
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 ;
let mut tree = new;
let mut div = tree.;
div.button.class.cdata;
assert_eq!;
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: