[][src]Crate ramhorns

Ramhorns logo


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?


use ramhorns::{Template, Content};

struct Post<'a> {
    title: &'a str,
    teaser: &'a str,

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}}<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>\
                          <h2>How I tried Ramhorns and found love 💖</h2>\
                          <p>This can happen to you too</p>\
                          <h2>Rust is kinda awesome</h2>\
                          <p>Yes, even the borrow checker! 🦀</p>\


pub use ramhorns_derive::Content;



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



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.



Error type used that can be emitted during template parsing.



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.