| Crates.io | htmf |
| lib.rs | htmf |
| version | 0.2.0 |
| created_at | 2025-01-21 13:51:58.502315+00 |
| updated_at | 2025-01-21 15:20:41.268715+00 |
| description | hypertext markup functions: functions for generating HTML |
| homepage | https://github.com/raffomania/htmf |
| repository | https://github.com/raffomania/htmf |
| max_upload_size | |
| id | 1524917 |
| size | 75,178 |
⚠️ Warning: This is alpha-quality, completely insecure software.
Functions for generating HTML from Rust in a declarative fashion:
use htmf::prelude::*;
a([href("https://www.rafa.ee"), class("link")])
.with(text("My Site"))
.to_html();
Coming soon: ways to import htmf, defining attributes, defining contents, converting to html.
After adding htmf to your dependencies, you can get started by declaring a document:
use htmf::prelude::*;
document().with(html([]).with([
head([]).with([
meta([
name("viewport"),
content("width=device-width,initial-scale=1")
]),
]),
body([class("mx-auto mw-xl")])
.with(main_([]))
]));
_ in their name (e.g. main_).with method. You can pass in arrays of elements, or a single element.If you prefer to avoid glob imports, you can import htmf modules explicitly:
use htmf::prelude as h;
h::document();
The with method accepts anything implementing the IntoElements trait, including single items implementing Into<Element>:
use htmf::prelude::*;
// Arrays
ul([]).with([li([]), li([])]);
// Single elements
div([]).with(p([]));
// Vectors
ul([]).with(vec![li([]), li([])]);
// Strings
p([]).with("I'm a string!");
p([]).with("I'm a string!".to_string());
// ()
div([]).with(());
// Options
div([]).with(Some(button([])));
div([]).with(None);
Tag functions accept anything implementing the IntoAttrs trait:
use htmf::prelude::*;
// Arrays
p([class("prose")]);
// Single attributes
p(class("prose"));
// ()
p(());
// Options
p(Some(class("prose")));
p(None);
Coming soon: 2 space indents, structuring code, unstable multiline string formatting.
writeln! macro to build HTML in a string bufferFor benchmarks, install cargo-criterion, e.g. using cargo install cargo criterion.
For working on the examples, install a nightly rust toolchain to use the unstable format_strings rustfmt option.