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.
The implementation is at a very early stage and the API is a subject of changes.
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:
rust
extern crate incrust;
All examples assume a prepared instance of Incrust. For ease of use hashmaps, we use the macro maplit
```rust
extern crate maplit; extern crate incrust;
use incrust::{Incrust, Args, Var};
fn main() { let incrust = Incrust::new(); // ... } ```
rust
let result = incrust.render_text("Hello, {{name}}!", &hashmap!{ "name" => Var::ex("World") }.unwrap();
assert_eq!(result, "Hello, World!");
rust
let args: Args = hashmap!{ "text" => Var::ex("<Cats & Dogs>") };
let result = incrust.render_text("<h1>{{ text | e }}</h1>", &args).unwrap();
assert_eq!(result, "<h1><Cats & Dogs></h1>");
rust
assert_eq!("Braces: {{", incrust.render_text(r#"Braces: {{ "{{" }}"#, &hashmap!{}).unwrap());
assert_eq!("Pi: 3.1415926", incrust.render_text(r#"Pi: {{ 3.1415926 }}"#, &hashmap!{}).unwrap());
```rust let args = hashmap!{ "what" => ex("Hello"), "who" => ex("World") }; asserteq!(r#"Say: "Hello, World!""#, incrust.rendertext(r#"Say: "{{ what + ", " + who }}!""#, args).unwrap());
let args = hashmap!{ "alpha" => ex(6isize), "omega" => ex(7f64) }; asserteq!("The answer is 42", incrust.rendertext(r#"The answer is {{ alpha * omega }}"#, args).unwrap()); ```
rust
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>");
rust
let tpl = "Example: {% raw %}{{ mustaches }}{% endraw %}";
let result = incrust.render_text(tpl, &hashmap!{}).unwrap();
assert_eq!(result, "Example: {{ mustaches }}");
Licensed under either of * Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0) * MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT) at your option.
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.