Crate gen_html

Source
Expand description

gen-html is a templating library for generating HTML from Rust.

§Features

  • Fasthtml! macro generates code that is as fast as writing to a string by hand.
  • Conditional rendering — you can use if, for and match inside your templates.
  • Automatic escaping, however you can opt-out using Raw<T>.
  • Type safety — HTML tags and attributes are checked at compile time.
  • Integration with the rust web ecosystem (axum, actix-web).

§Example

use gen_html::html;

let markup = html! {
    for i in 1..=3 {
        span { (i.to_string()) }
    }
};

println!("{}", markup);

The html! macro roughly expands to this code.

use gen_html::{Render, render_fn};

let markup = render_fn(|f| {
    for i in 1..=3 {
        f.write_str("<span>")?;
        (&i.to_string()).render_to(f)?;
        f.write_str("</span>")?;
    }
    Ok(())
});

/* ... */

Macros§

html
Generate HTML with maud-like syntax.

Structs§

Escaped
Wrapper that escapes HTML special characters.
Raw
Wrapper to render content using Display without escaping.
RenderFn
Implements Render using a function.

Constants§

DOCTYPE
The <!DOCTYPE html> string literal.

Traits§

Render
Trait for safely rendering HTML content.

Functions§

render_fn
Creates a type whose Render impl is provided with the function f.