Crates.io | html-string |
lib.rs | html-string |
version | 0.3.0 |
source | src |
created_at | 2023-05-29 14:39:51.393998 |
updated_at | 2023-09-20 19:29:10.892432 |
description | Simple server-side html generation |
homepage | |
repository | https://gitlab.com/wake-sleeper/html-string |
max_upload_size | |
id | 877166 |
size | 23,850 |
Simple, server-side html generation in Rust.
Warning: WIP
Use functions named after html tags:
use html_string::tags::*;
let html = html([head(()), body(div(()))]);
assert_eq!(
format!("{html}"),
"<html><head></head><body><div></div></body></html>"
)
Tag functions are very flexible in the type of arguments you can provide:
use html_string::tags::*;
use html_string::attr;
// Empty node
div(());
// Another html node
div(div(()));
// A list of nodes
div([p(()), p(())]);
// Just text
div("foot");
// `Strings` also work
div(String::from("hello"));
// Attributes
div(attr! { "class" => "box", "id" => "box-1" });
// Attributes and text (notice the tuple)
div((attr! { "class" => "box" }, "foo"));
// Attributes and a node
div((attr! { "class" => "box" }, div(())));
// Attributes and a list of nodes
div((attr! { "class" => "box" }, [p(()), p(())]));
Why use a template language when you are already using rust:
use html_string::tags::*;
use html_string::Node;
let items = vec!["apples", "oranges", "books"];
let list = ul(items.into_iter().map(|i| li(i)).collect::<Vec<Node>>());
assert_eq!(
format!("{list}"),
"<ul><li>apples</li><li>oranges</li><li>books</li></ul>"
);