12 releases (6 breaking)

Uses old Rust 2015

0.9.0 Feb 12, 2018
0.8.2 Oct 11, 2017
0.8.1 Aug 3, 2017
0.8.0 Nov 14, 2016
0.4.0 Dec 14, 2014

#63 in Template engine

Download history 6228/week @ 2023-12-15 4215/week @ 2023-12-22 4517/week @ 2023-12-29 6881/week @ 2024-01-05 6535/week @ 2024-01-12 5477/week @ 2024-01-19 5811/week @ 2024-01-26 5608/week @ 2024-02-02 6161/week @ 2024-02-09 6275/week @ 2024-02-16 6402/week @ 2024-02-23 6326/week @ 2024-03-01 7102/week @ 2024-03-08 7267/week @ 2024-03-15 8772/week @ 2024-03-22 7788/week @ 2024-03-29

32,107 downloads per month
Used in 78 crates (29 directly)

MIT/Apache

77KB
2K SLoC

Mustache Ohloh statistics Build Status

Inspired by ctemplate and et, Mustache is a framework-agnostic way to render logic-free views.

As ctemplates says, "It emphasizes separating logic from presentation: it is impossible to embed application logic in this template language."

rust-mustache is a rust implementation of Mustache.

Documentation

The different Mustache tags are documented at mustache(5).

Documentation for this library is here.

Install

Install it through Cargo!

[dependencies]
mustache = "*"

Basic example

#[macro_use]
extern crate serde_derive;
extern crate mustache;

use std::io;
use mustache::MapBuilder;

#[derive(Serialize)]
struct Planet {
    name: String,
}

fn main() {
    // First the string needs to be compiled.
    let template = mustache::compile_str("hello {{name}}").unwrap();

    // You can either use an encodable type to print "hello Mercury".
    let planet = Planet { name: "Mercury".into() };
    template.render(&mut io::stdout(), &planet).unwrap();
    println!("");

    // ... or you can use a builder to print "hello Venus".
    let data = MapBuilder::new()
        .insert_str("name", "Venus")
        .build();

    template.render_data(&mut io::stdout(), &data).unwrap();
    println!("");

    // ... you can even use closures.
    let mut planets = vec!("Jupiter", "Mars", "Earth");

    let data = MapBuilder::new()
        .insert_fn("name", move |_| {
            planets.pop().unwrap().into()
        })
        .build();

    // prints "hello Earth"
    template.render_data(&mut io::stdout(), &data).unwrap();
    println!("");

    // prints "hello Mars"
    template.render_data(&mut io::stdout(), &data).unwrap();
    println!("");

    // prints "hello Jupiter"
    template.render_data(&mut io::stdout(), &data).unwrap();
    println!("");
}

Testing

Simply clone and run:

# If you want to run the test cases, you'll need the spec as well.
git submodule init
git submodule update

cargo test

# If you want to test the readme example, we're currently using the unstable feature to do so.
cargo +nightly test --features unstable

License

See LICENSE File

Dependencies

~265–510KB
~11K SLoC