incrust 0.0.15

Template engine inspired by Jinja2
Documentation

{% Incrust %}

Incrust is a template engine inspired by Jinja2 and written in Rust.

NOTA BENE

This is a work in progress and (as may be assumed reasonably enough) may be highly unstable just yet.

Unstable

The implementation is at a very early stage and the API is a subject of changes.

Since Incrust developed with unstable features it depends on nightly Rust. Release version will be refactored to build on stable channel.

Installation

Incrust is available on crates.io and can be included in your Cargo enabled project like this:

[dependencies]
incrust = "0.0"

Then include it in your code like this:

extern crate incrust;

Examples

All examples assume a prepared instance of Incrust. For ease of use hashmaps, we use the macro maplit

#[macro_use]
extern crate maplit;
extern crate incrust;

use incrust::{Incrust, Args, Var};

fn main() {
    let incrust = Incrust::new();
    // ...
}

Variables

let result = incrust.render_text("Hello, {{name}}!", hashmap!{ "name" => ex("World") }.unwrap();
assert_eq!(result, "Hello, World!");

Filters

let args: Args = hashmap!{ "text" => ex("<Cats & Dogs>") };
let result = incrust.render_text("<h1>{{ text | e }}</h1>", args).unwrap();
assert_eq!(result, "<h1>&lt;Cats &amp; Dogs&gt;</h1>");

Literals

assert_eq!("Braces: {{", incrust.render_text(r#"Braces: {{ "{{" }}"#, hashmap!{}).unwrap());
assert_eq!("Pi: 3.1415926", incrust.render_text(r#"Pi: {{ 3.1415926 }}"#, hashmap!{}).unwrap());

Expressions

let args = hashmap!{
    "what" => ex("Hello"),
    "who" => ex("World")
};
assert_eq!(r#"Say: "Hello, World!""#, incrust.render_text(r#"Say: "{{ what + ", " + who }}!""#, args).unwrap());

let args = hashmap!{
    "alpha" => ex(6isize),
    "omega" => ex(7f64)
};
assert_eq!("The answer is 42", incrust.render_text(r#"The answer is {{ alpha * omega }}"#, args).unwrap());

Lazy boolean evaluation

let tpl = r#"Amount: {{ amount and ("" + amount + " pcs") or "-" }}"#;
assert_eq!("Amount: 6 pcs", incrust.render_text(tpl, hashmap!{ "amount" => ex(6isize) }).unwrap());
assert_eq!("Amount: -", incrust.render_text(tpl, hashmap!{ "amount" => ex(0isize) }).unwrap());

Conditional statements

assert_eq!("String is empty", incrust.render_text(r#"String {% if "" %}has chars{% else %}is empty{% endif %}"#, hashmap!{}).unwrap());
assert_eq!("It's true", incrust.render_text(r#"It's {% if False %}false{% elif True %}true{% endif %}"#, hashmap!{}).unwrap());

For-Loop statements

let args = hashmap!{ "fruits" => ex(vec![ex("Orange"), ex("Apple"), ex("Banana")]) };
let tpl = r#"<ul>{% for fruit in fruits %}<li>{{ index }}. {{ fruit | e }}</li>{% endfor %}</ul>"#;
let expected = r#"<ul><li>1. Orange</li><li>2. Apple</li><li>3. Banana</li></ul>"#;
assert_eq!(expected, incrust.render_text(tpl, args).unwrap());

Comments

let tpl = incrust.parse("<p>Visible {# partially #} paragraph</p>").unwrap();
let result = incrust.render_parsed(tpl, hashmap!{}).unwrap();
assert_eq!(result, "<p>Visible  paragraph</p>");

Escaping

let tpl = "Example: {% raw %}{{ mustaches }}{% endraw %}";
let result = incrust.render_text(tpl, hashmap!{}).unwrap();
assert_eq!(result, "Example: {{ mustaches }}");

License

Licensed under either of

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.