3 releases

new 0.1.2 Dec 14, 2024
0.1.1 Dec 7, 2024
0.1.0 Dec 2, 2024

#510 in Template engine

Download history 113/week @ 2024-11-29 176/week @ 2024-12-06 136/week @ 2024-12-13

425 downloads per month

MIT/Apache

69KB
1.5K SLoC

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::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();

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", {
      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.

Dependencies

~0–385KB