#html #web #ssr #html-templating #template

ryde_html

An ergonomic way to render html using plain old rust

2 releases

0.2.1 Mar 18, 2024
0.2.0 Feb 29, 2024

#420 in Template engine

Download history 147/week @ 2024-02-26 13/week @ 2024-03-04 5/week @ 2024-03-11 147/week @ 2024-03-18 30/week @ 2024-04-01

183 downloads per month
Used in 2 crates

MIT license

20KB
549 lines

html

html offers an ergonomic way to render html from plain rust functions

cargo add html

Write some html

use html::*;

fn render(element: Element) -> String {
  html::render((
    doctype(),
    html((
      head((title("title"), meta().charset("utf-8"))),
      body(element)
    ))
  ))
}

#[cfg(test)]
mod tests {

  #[test]
  fn it_works() {
      assert_eq!(
        render(div("html")),
        "<!DOCTYPE html><html><head><title>title</title></head><body><div>html</div></body></html>"
      )
  }
}

Custom attributes

use html::*;

fn htmx_input() -> Element {
  input()
    .attr("hx-post", "/")
    .attr("hx-target", ".target")
    .attr("hx-swap", "outerHTML")
    .attr("hx-push-url", "false")
}

fn main() {
  let html: String = render(htmx_input());
  // html == <input hx-post="/" hx-target=".target" hx-swap="outerHTML" hx-push-url="false">
}

Custom elements

use html::*;

fn turbo_frame(children: Element) -> Element {
    element("turbo-frame", children)
}

fn main() {
  let html: String = render(turbo_frame(div("inside turbo frame")).id("id"));
  // <turbo-frame id="id">
  //   <div>inside turbo frame</div>
  // </turbo-frame>
}

Dependencies

~1.7–2.4MB
~47K SLoC