#template #html #markup #pug #haml

markup

A blazing fast, type-safe template engine for Rust

26 releases (11 breaking)

0.12.0 Mar 28, 2021
0.11.0 Oct 3, 2020
0.10.0 Oct 2, 2020
0.4.1 Sep 21, 2019
0.1.1 Dec 3, 2018

#16 in Template engine

Download history 98/week @ 2020-12-24 215/week @ 2020-12-31 243/week @ 2021-01-07 121/week @ 2021-01-14 93/week @ 2021-01-21 94/week @ 2021-01-28 157/week @ 2021-02-04 526/week @ 2021-02-11 150/week @ 2021-02-18 115/week @ 2021-02-25 96/week @ 2021-03-04 107/week @ 2021-03-11 91/week @ 2021-03-18 247/week @ 2021-03-25 167/week @ 2021-04-01 149/week @ 2021-04-08

716 downloads per month
Used in freedesktop-desktop-entry

MIT/Apache

12KB
254 lines

markup.rs

A blazing fast, type-safe template engine for Rust.

Build Version Downloads License

markup.rs is a template engine for Rust powered by procedural macros which parses the template at compile time and generates optimal Rust code to render the template at run time. The templates may embed Rust code which is type checked by the Rust compiler enabling full type-safety.

Features

  • Fully type-safe with inline highlighted errors when using editor extensions like rust-analyzer.
  • Less error-prone and terse syntax inspired by Haml, Slim, and Pug.
  • Zero unsafe code.
  • Zero runtime dependencies.
  • ⚡ Blazing fast. The fastest in this benchmark among the ones which do not use unsafe code, the second fastest overall.

Install

[dependencies]
markup = "0.12.0"

Example

markup::define! {
    Home<'a>(title: &'a str) {
        @markup::doctype()
        html {
            head {
                title { @title }
                style {
                    "body { background: #fafbfc; }"
                    "#main { padding: 2rem; }"
                }
            }
            body {
                @Header { title }
                #main {
                    p {
                        "This domain is for use in illustrative examples in documents. You may \
                        use this domain in literature without prior coordination or asking for \
                        permission."
                    }
                    p {
                        a[href = "https://www.iana.org/domains/example"] {
                            "More information..."
                        }
                    }
                }
                @Footer { year: 2020 }
            }
        }
    }

    Header<'a>(title: &'a str) {
        header {
            h1 { @title }
        }
    }

    Footer(year: u32) {
        footer {
            "(c) " @year
        }
    }
}

fn main() {
    println!(
        "{}",
        Home {
            title: "Example Domain"
        }
    )
}

Output (manually prettified)

<!DOCTYPE html>
<html>
  <head>
    <title>Example Domain</title>
    <style>
      body {
        background: #fafbfc;
      }
      #main {
        padding: 2rem;
      }
    </style>
  </head>
  <body>
    <header><h1>Example Domain</h1></header>
    <div id="main">
      <p>
        This domain is for use in illustrative examples in documents. You may
        use this domain in literature without prior coordination or asking for
        permission.
      </p>
      <p>
        <a href="https://www.iana.org/domains/example">More information...</a>
      </p>
    </div>
    <footer>(c) 2020</footer>
  </body>
</html>

Dependencies

~360–790KB
~20K SLoC