| Crates.io | el |
| lib.rs | el |
| version | 0.2.0 |
| created_at | 2024-12-02 20:25:24.368838+00 |
| updated_at | 2025-01-01 21:43:31.631438+00 |
| description | Write and manipulate HTML elements as data |
| homepage | |
| repository | https://github.com/Garmelon/el |
| max_upload_size | |
| id | 1469172 |
| size | 83,820 |
el is a no-dependencies Rust library for writing, modifying, and safely
rendering HTML elements as simple data structures. It is inspired by hiccup
and named after a small helper function I once wrote in JS.
use el::{Render, html::*};
let page: String = html((
head((
meta((
attr::name("viewport"),
attr::content("width=device-width, initial-scale=1"),
)),
title("Example page"),
)),
body((
h1((attr::id("heading"), "Example page")),
p(("This is an example for a ", em("simple"), " web page.")),
)),
))
.into_document()
.render_to_string()
.unwrap();
See the top-level crate documentation for more info.
Here it is in full, for posteriority:
function el(name, attributes, ...children) {
const element = document.createElement(name);
for (const [name, value] of Object.entries(attributes))
element.setAttribute(name, value);
element.append(...children);
return element;
}
Use it like so:
const page = el("html", {},
el("head", {},
el("meta", {
name: "viewport",
content: "width=device-width, initial-scale=1",
}),
el("title", {}, "Example page"),
),
el("body", {},
el("h1", { id: "heading" }, "Example page"),
el("p", {}, "This is an example for a ", el("em", {}, "simple"), " web page."),
),
);
This entire project is dual-licensed under the Apache 2.0 and MIT licenses.