htmlm

Crates.iohtmlm
lib.rshtmlm
version0.5.0
sourcesrc
created_at2024-10-07 10:32:40.566703
updated_at2024-10-10 12:32:17.657358
descriptionhtml macro with no dependencies
homepage
repository
max_upload_size
id1399826
size3,955
Jakub Dóka (jakubDoka)

documentation

README

htmlm (for a lack of free names)

This crate implements simple html template macros without any dependencies. Use at your own risk.

Example

let escape = htmlm::html! {
    {"<script>alert('css')</script>"}
};
assert_eq!(escape, "&lt;script&gt;alert(&#039;css&#039;)&lt;/script&gt;");

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>");
Commit count: 0

cargo fmt