mustache template library for rust (customization fork)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
Go to file
Shad Amethyst 6e5c901a09
🐛 Fix MapBuilder using the wrong Fn traits
2 years ago
examples doc(examples): add compile_path example 7 years ago
spec@9b1bc7ad19 Import the mustache specs 13 years ago
src 🐛 Fix MapBuilder using the wrong Fn traits 2 years ago
tests Use a generic method for handling partials 2 years ago
.gitignore Modernize a little bit! 10 years ago
.gitmodules Import the mustache specs 13 years ago
.travis.yml Eliminate panics from live code (#73) 3 years ago
Cargo.toml 🐛 Fix MapBuilder using the wrong Fn traits 2 years ago
LICENSE Initial import 13 years ago
README.md Use a generic method for handling partials 2 years ago
rustfmt.toml style(*): rustfmt 8 years ago

README.md

Mustache (customization fork)

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

Releasing

If cutting a new release, please follow something along the lines of the below:

# Assuming master is the current release commit, ideally it will be a commit
# announcing the release and the only change would be the version number.

# Ensure everything looks good
cargo publish --dry-run

# Actually publish
cargo publish

# Tag the release, prefix it with 'v' for easy tag searching, i.e. git tag --list 'v*'
git tag vX.Y.Z

git push --tags origin master

License

See LICENSE File