Coder Social home page Coder Social logo

maciejhirsz / ramhorns Goto Github PK

View Code? Open in Web Editor NEW
284.0 7.0 29.0 305 KB

Fast Mustache template engine implementation in pure Rust.

Home Page: https://crates.io/crates/ramhorns

License: Mozilla Public License 2.0

Rust 99.57% HTML 0.41% CSS 0.02%
rust templating-engine templates mustache mustache-templates handlebars

ramhorns's Introduction

Ramhorns logo

Ramhorns

Tests badge Crates.io version badge Docs Crates.io license badge

Fast Mustache template engine implementation in pure Rust.

Ramhorns loads and processes templates at runtime. It comes with a derive macro which allows for templates to be rendered from native Rust data structures without doing temporary allocations, intermediate HashMaps or what have you.

With a touch of magic ๐ŸŽฉ, the power of friendship ๐Ÿฅ‚, and a sparkle of FNV hashing โœจ, render times easily compete with static template engines like Askama.

Cargo.toml

[dependencies]
ramhorns = "0.5"

Example

use ramhorns::{Template, Content};

#[derive(Content)]
struct Post<'a> {
    title: &'a str,
    teaser: &'a str,
}

#[derive(Content)]
struct Blog<'a> {
    title: String,        // Strings are cool
    posts: Vec<Post<'a>>, // &'a [Post<'a>] would work too
}

// Standard Mustache action here
let source = "<h1>{{title}}</h1>\
              {{#posts}}<article><h2>{{title}}</h2><p>{{teaser}}</p></article>{{/posts}}\
              {{^posts}}<p>No posts yet :(</p>{{/posts}}";

let tpl = Template::new(source).unwrap();

let rendered = tpl.render(&Blog {
    title: "My Awesome Blog!".to_string(),
    posts: vec![
        Post {
            title: "How I tried Ramhorns and found love ๐Ÿ’–",
            teaser: "This can happen to you too",
        },
        Post {
            title: "Rust is kinda awesome",
            teaser: "Yes, even the borrow checker! ๐Ÿฆ€",
        },
    ]
});

assert_eq!(rendered, "<h1>My Awesome Blog!</h1>\
                      <article>\
                          <h2>How I tried Ramhorns and found love ๐Ÿ’–</h2>\
                          <p>This can happen to you too</p>\
                      </article>\
                      <article>\
                          <h2>Rust is kinda awesome</h2>\
                          <p>Yes, even the borrow checker! ๐Ÿฆ€</p>\
                      </article>");

Features

  • Rendering common types, such as &str, String, bools, and numbers into {{variables}}.
  • Unescaped printing with {{{tripple-brace}}} or {{&ampersant}}.
  • Rendering sections {{#foo}} ... {{/foo}}.
  • Rendering inverse sections {{^foo}} ... {{/foo}}.
  • Rendering partials {{>file.html}}.
  • Zero-copy CommonMark rendering from fields marked with #[md].

Benches

Rendering a tiny template:

test a_simple_ramhorns            ... bench:          82 ns/iter (+/- 4) = 1182 MB/s
test b_simple_askama              ... bench:         178 ns/iter (+/- 8) = 544 MB/s
test c_simple_tera                ... bench:         416 ns/iter (+/- 98) = 233 MB/s
test c_simple_tera_from_serialize ... bench:         616 ns/iter (+/- 33) = 157 MB/s
test d_simple_mustache            ... bench:         613 ns/iter (+/- 34) = 158 MB/s
test e_simple_handlebars          ... bench:         847 ns/iter (+/- 40) = 114 MB/s

Rendering a tiny template with partials:

test pa_partials_ramhorns         ... bench:          85 ns/iter (+/- 7) = 1141 MB/s
test pb_partials_askama           ... bench:         210 ns/iter (+/- 9) = 461 MB/s
test pc_partials_mustache         ... bench:         827 ns/iter (+/- 39) = 117 MB/s
test pd_partials_handlebars       ... bench:         846 ns/iter (+/- 29) = 114 MB/s

Compiling a template from a string:

test xa_parse_ramhorns            ... bench:         190 ns/iter (+/- 10) = 821 MB/s
test xb_parse_mustache            ... bench:       3,229 ns/iter (+/- 159) = 48 MB/s
test xe_parse_handlebars          ... bench:       6,883 ns/iter (+/- 383) = 22 MB/s

Worth noting here is that Askama is processing templates at compile time and generates static rust code for rendering. This is great for performance, but it also means you can't swap out templates without recompiling your Rust binaries. In some cases, like for a static site generator, this is unfortunately a deal breaker.

Parsing the templates on runtime is never going to be free, however Ramhorns has a really fast parser built on top of Logos, that makes even that part of the process snappy.

The Mustache crate is the closest thing to Ramhorns in design and feature set.

License

Ramhorns is free software, and is released under the terms of the Mozilla Public License version 2.0. See LICENSE.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.