html-string

Crates.iohtml-string
lib.rshtml-string
version0.3.0
sourcesrc
created_at2023-05-29 14:39:51.393998
updated_at2023-09-20 19:29:10.892432
descriptionSimple server-side html generation
homepage
repositoryhttps://gitlab.com/wake-sleeper/html-string
max_upload_size
id877166
size23,850
Eze Anyanwu (eze-works)

documentation

README

html-string

Simple, server-side html generation in Rust.

Warning: WIP

Examples

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

cargo fmt