macro_rules! html {
($($body:tt)*) => { ... };
}Expand description
Builds a node tree from nested markup-like syntax.
§Syntax
| form | meaning |
|---|---|
div { … } | an element with children |
div(class = "card", id = "x") { … } | attributes, then children |
input(type = "email", required) | a bare name is a boolean attribute |
"text" | escaped text |
(expr) | an escaped Display expression |
raw(expr) | unescaped markup |
@if cond { … } @else { … } | conditional, empty branch renders to nothing |
@for pat in iter { … } | repetition |
§Examples
use winged_rust::{html, prelude::*};
let page = html! {
div(class = "card", id = "hero") {
h1 { "Welcome" }
p { "Fuel, tyres & chain." }
}
};
assert_eq!(
page.render(),
r#"<div class="card" id="hero"><h1>Welcome</h1><p>Fuel, tyres & chain.</p></div>"#
);Conditionals and loops, matching buildOptional / buildEither / buildArray:
use winged_rust::{html, prelude::*};
let names = ["Ana", "Bruno"];
let logged_in = false;
let list = html! {
ul {
@for name in names { li { (name) } }
@if logged_in { li { "Sign out" } }
}
};
assert_eq!(list.render(), "<ul><li>Ana</li><li>Bruno</li></ul>");The expansion is the builder API, so the two are interchangeable:
use winged_rust::{html, prelude::*};
assert_eq!(
html! { p(class = "lead") { "hi" } }.render(),
Node::from(p().add_class("lead").child(Node::text("hi"))).render()
);