[][src]Crate ramhorns

Ramhorns logo

Ramhorns

Experimental 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.

What else do you want, a sticker?

Example

use ramhorns::{Template, Context};

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

#[derive(Context)]
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);

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>");

Re-exports

pub use ramhorns_derive::Context;

Structs

Encoder
Section
Template

Traits

Context

Functions

escape

Utility for writing slices into a buffer, escaping HTML characters