mustache2 1.0.5

Logic-less templates.
Documentation

This crate is a rewrite of the old mustache crate. It has a different API and is compliant with all* of the Mustache specification, including the optional modules.

Features

This crate supports all of the basic Mustache specification, as well as the inheritance, dynamic name, and lambda extensions. These can be enabled with Cargo feature flags.

[dependencies]
# Enable inheritance and dynamic names.
mustache2 = { version = "1.4.3", features = ["ext-inheritance", "ext-dyn-names"] }

Additionally, it has a number of feature flags that represent stylistic differences that deviate from the specification.

  • strict-resolver: Produces a compiler error whenever a partial tag cannot find a source file. This does not apply to dynamic names, if enabled.
  • inline-indentation: Applies indentation to non-standalone tags as well as standalone tags. See [inline-indentation-example.md] for more information.

Finally, the serde feature flag enables support for serde, including serializing and deserializing from data and inputting serde structs directly into the renderer.

Documentation

You can read how to use the Mustache language here on their website. Documentation for this crate is hosted on docs.rs.

Basic example

use std::collections::HashMap;

use mustache2::{
    Data,
    render::{RenderManager, SourceCache, provider::StaticProvider},
};

fn main() {
    // This object is required to satisfy the borrow checker, but you don't need to use it for anything else.
    let cache = SourceCache::default();
    let mut renderer = RenderManager::new(
        // You can use `FsProvider` to read files off the disk.
        StaticProvider(HashMap::from([("template", "Hello, {{name}}!\n")])),
        &cache,
    );
    let mut out = String::new();

    // Render the template, saying "Hello, Mercury!".
    if let Err(e) = renderer.render_to(
        &mut out,
        "template",
        Data::map_from([("name".into(), "Mercury".into())]),
    ) {
        renderer.display_error(e);
        return;
    }

    // Render it again, saying "Hello, Venus!".
    if let Err(e) = renderer.render_to(
        &mut out,
        "template",
        Data::map_from([("name".into(), "Venus".into())]),
    ) {
        renderer.display_error(e);
        return;
    }

    assert_eq!(out, "Hello, Mercury!\nHello, Venus!\n");
}