31 releases

0.12.5 Sep 11, 2021
0.12.2 May 24, 2021
0.12.0 Mar 28, 2021
0.11.0 Oct 3, 2020
0.1.1 Dec 3, 2018

#39 in Procedural macros

Download history 193/week @ 2021-06-30 179/week @ 2021-07-07 275/week @ 2021-07-14 146/week @ 2021-07-21 198/week @ 2021-07-28 201/week @ 2021-08-04 282/week @ 2021-08-11 293/week @ 2021-08-18 193/week @ 2021-08-25 284/week @ 2021-09-01 375/week @ 2021-09-08 332/week @ 2021-09-15 523/week @ 2021-09-22 319/week @ 2021-09-29 223/week @ 2021-10-06 331/week @ 2021-10-13

1,009 downloads per month
Used in 2 crates (via markup)

MIT/Apache

29KB
725 lines

markup.rs

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

Build Version Documentation 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.5"

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

<!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>

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

~310–730KB
~18K SLoC