Crate ramhorns[][src]

Expand description
Ramhorns logo

Ramhorns

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.

What else do you want, a sticker?

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

Modules

Utilities dealing with writing the bits of a template or data to the output and escaping special HTML characters.

This module contains helper traits that are used internally to manage sequences of types implementing the Content trait, allowing us to statically manage parent section lookups without doing extra work on runtime.

Structs

Aggregator for Templates, that allows them to be loaded from the file system and use partials: {{>partial}}

A section of a Template that can be rendered individually, usually delimited by {{#section}} ... {{/section}} tags.

A preprocessed form of the plain text template, ready to be rendered with data contained in types implementing the Content trait.

Enums

Error type used that can be emitted during template parsing.

Traits

Trait allowing the rendering to quickly access data stored in the type that implements it. You needn’t worry about implementing it, in virtually all cases the #[derive(Content)] attribute above your types should be sufficient.

Derive Macros