Crates.io | mix |
lib.rs | mix |
version | |
source | src |
created_at | 2024-02-03 21:59:14.93256 |
updated_at | 2025-01-30 13:44:35.119143 |
description | mix is a simple html templating engine for rust |
homepage | |
repository | |
max_upload_size | |
id | 1125679 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include` |
size | 0 |
A zero-dependency, lightweight HTML builder for Rust with an elegant macro syntax. Build HTML structures with the simplicity of JSX and the power of Rust - in just ~200 lines of code!
let page = html! {
div (class = "container") {
h1 { "Welcome!" }
p { "This is a " span (class = "highlight") { "simple" } " example." }
}
};
html! {
div (class = "card", id = "main") {
h2 { "Hello, World!" }
p { "This is a paragraph." }
}
}
struct Card {
title: String,
content: String,
}
impl Html for Card {
fn render(&self) -> String {
html! {
div (class = "card") {
h3 { (self.title) }
p { (self.content) }
}
}
}
}
// Use it in your HTML:
let card = Card {
title: "My Card".into(),
content: "Some content".into(),
};
html! {
div {
(card)
}
}
html! {
nav (class = "navbar") {
ul {
li { a (href = "/") { "Home" } }
li { a (href = "/about") { "About" } }
li { a (href = "/contact") { "Contact" } }
}
}
}
The entire implementation is built around just three core components:
Html
trait:pub trait Html {
fn render(&self) -> String;
}
Element
struct for building HTML elements:pub struct Element {
tag: String,
attrs: Vec<(String, String)>,
children: Vec<Box<dyn Html>>,
}
html!
macro that makes it all work together seamlesslyMIT License
Contributions are welcome! Feel free to open issues and pull requests.