2 releases (1 stable)

1.0.0 Dec 6, 2020
0.1.0 Dec 5, 2020

#340 in Template engine

Apache-2.0

15KB
196 lines

Funcy Crates.io Status Documentation

Funcy is a simple function based template engine.

Examples

Echo

struct Echo();
impl funcy::PlaceholderFunction for Echo {
    fn placeholder_fn_handler<'a>(&mut self, _name: &'a str, arg: &'a str) -> Result<String, String> {
        Ok(arg.to_string())
    }
}

let mut tr = funcy::TemplateRenderer::with_template("<!$ echo Hello>, World!");
tr.set_placeholder_fn("echo", Box::new(Echo()));
assert_eq!(tr.render().unwrap(), "Hello, World!");

Counter

struct Counter(usize);
impl funcy::PlaceholderFunction for Counter {
    fn placeholder_fn_handler<'a>(&mut self, _name: &'a str, _arg: &'a str) -> Result<String, String> {
        self.0 += 1;
        Ok(self.0.to_string())
    }
}

let mut tr = funcy::TemplateRenderer::with_template("<!$ counter> <!$ counter> <!$ counter>")
let counter = Counter(0);
tr.set_placeholder_fn("counter", Box::new(counter));
assert_eq!(tr.render().unwrap(), "1 2 3");

License

Funcy is distributed under the Apache-2.0 license. See the LICENSE file.

No runtime deps