3 releases (breaking)

0.3.0 Sep 20, 2023
0.2.0 Sep 3, 2023
0.1.0 May 29, 2023

#370 in Template engine

46 downloads per month

MIT license

23KB
443 lines

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>"
);

Dependencies

~14KB