el

Crates.ioel
lib.rsel
version
sourcesrc
created_at2024-12-02 20:25:24.368838
updated_at2024-12-07 23:25:42.605022
descriptionWrite and manipulate HTML elements as data
homepage
repositoryhttps://github.com/Garmelon/el
max_upload_size
id1469172
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`
size0
(Garmelon)

documentation

README

el

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.

Show me a simple example

use el::{Attr, Render, html::*};

let page: String = html((
    head((
        meta(Attr::new("charset", "utf-8")),
        meta((
            Attr::new("name", "viewport"),
            Attr::new("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();

What now?

See the top-level crate documentation for more info.

But what about that small helper function?

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", { charset: "utf-8" }),
    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."),
  ),
);

License

This entire project is dual-licensed under the Apache 2.0 and MIT licenses.

Commit count: 30

cargo fmt