Crates.io | htmlm |
lib.rs | htmlm |
version | 0.5.0 |
source | src |
created_at | 2024-10-07 10:32:40.566703 |
updated_at | 2024-10-10 12:32:17.657358 |
description | html macro with no dependencies |
homepage | |
repository | |
max_upload_size | |
id | 1399826 |
size | 3,955 |
This crate implements simple html template macros without any dependencies. Use at your own risk.
let escape = htmlm::html! {
{"<script>alert('css')</script>"}
};
assert_eq!(escape, "<script>alert('css')</script>");
let no_escape = htmlm::html! {
!{"<script>alert('css')</script>"}
};
assert_eq!(no_escape, "<script>alert('css')</script>");
let if_expr = htmlm::html! {
<div>
if let Some(y) = Some("yeh") {
y
} else {
"nah"
}
</div>
};
assert_eq!(if_expr, "<div>yeh</div>");
let match_expr = htmlm::html! {
<div>
match true {
false => { "nah" }
trye => { "yeh" }
}
</div>
};
assert_eq!(match_expr, "<div>yeh</div>");
let for_expr = htmlm::html! {
<div>
for n in ["a", "b", "c"] {
<div>!n</div>
} else {
"no content"
}
</div>
};
assert_eq!(for_expr, "<div><div>a</div><div>b</div><div>c</div></div>");
fn component(buf: &mut String, hello: &str) {
htmlm::write_html! { (buf)
<span>hello</span>
}
}
use std::fmt::Write;
let mut buf = String::new();
htmlm::write_html! { (buf)
<div>|f|{component(f, "hello")}" there"</div>
}
assert_eq!(buf, "<div><span>hello</span> there</div>");