Expand description
A HTML to Node macro powered by rstml.
Values returned from braced blocks ({ ... }) are expected to return
something that implements Into<Node>. This is already implemented for
anything that implements IntoIterator<Item = Node>, so you
can return something like a Vec<Node> or an
Iterator<Item = Node> directly.
Due to Rust’s trait implementation rules, you cannot directly return
Strings. Instead, you can use the text! macro to convert the
String to a Node::Text.
Node implements Display (and by extension
ToString), so you can turn it into a string representation easily using
Node::to_string().
See the rstml docs for supported tags and syntax.
Examples
Basic
use html_node::{html, text};
let shopping_list = vec!["milk", "eggs", "bread"];
let html = html! {
<div>
<h1>Shopping List</h1>
<ul>
{ shopping_list.into_iter().zip(1..).map(|(item, i)| html! {
<li class="item">
<input type="checkbox" id={format!("item-{i}")}>
<label for={format!("item-{i}")}>{text!("{item}")}</label>
</li>
}) }
</ul>
</div>
};
let expected = "\
<div>\
<h1>Shopping List</h1>\
<ul>\
<li class=\"item\">\
<input type=\"checkbox\" id=\"item-1\">\
<label for=\"item-1\">milk</label>\
</li>\
<li class=\"item\">\
<input type=\"checkbox\" id=\"item-2\">\
<label for=\"item-2\">eggs</label>\
</li>\
<li class=\"item\">\
<input type=\"checkbox\" id=\"item-3\">\
<label for=\"item-3\">bread</label>\
</li>\
</ul>\
</div>\
";
assert_eq!(html.to_string(), expected);Pretty-Printing
use html_node::{html, text};
let html = html! {
<div>
<h1>Shopping List</h1>
<ul>
<li>Eggs</li>
<li>Milk</li>
<li>Bread</li>
</ul>
</div>
};
let expected = "\
<div>
<h1>
Shopping List
</h1>
<ul>
<li>
Eggs
</li>
<li>
Milk
</li>
<li>
Bread
</li>
</ul>
</div>\
";
// note the `#` in the format string, which enables pretty-printing
let formatted_html = format!("{html:#}");
assert_eq!(formatted_html, expected);Modules
- typed
typedTyped HTML nodes.
Macros
- Creates a
Node::Comment. - The HTML to
Nodemacro. - Creates a
Node::Text. - Creates a
Node::UnsafeText.
Structs
- A comment.
- A doctype.
- An element.
- A fragment.
- A text node.
- An unsafe text node.
Enums
- An HTML node.