Incrust is a template engine inspired by Jinja2 and written in Rust.
In fact it is a Jinja2, Django, Twig, Swig, Liquid (and probably others) template engines constellation, which uses similar methodologies.
The implementation is at a very early stage and the API is a subject of changes.
Note that Incrust currently requires the nightly version of the Rust compiler.
Incrust is available on crates.io and can be included in your Cargo enabled project like this:
[dependencies]
incrust = "0.2"
For ease of use hashmaps you may use the maplit
Then you need to setup your environment:
```rust
extern crate maplit; extern crate incrust;
use incrust::ex;
fn create_env<'a>() -> Incrust<'a> { use incrust::{Incrust, FilesystemLoader};
let mut instance = Incrust::default();
instance.loaders.push(FilesystemLoader::new("./assets/tpl"));
instance
}
fn main() { let incrust = create_env(); let args = hashmap!{ "name".into() => ex("World") }; incrust.render("hello", args).unwrap(); } ```
Though Incrust has smart loaders, it may be used just as advanced formatter to render directly from string template
rust
incrust.render_text("Hello, {{ name | e }}!", hashmap!{ "name".into() => ex("World") }).unwrap();
// or with prepared template
let hello = incrust.parse("Hello, {{ name | e }}!");
let args = hashmap!{ "name".into() => ex("World") };
incrust.render_parsed(&hello, args).unwrap();
rust
let args = hashmap!{ "title".into() => ex("<Cats & Dogs>") };
```twig
html
```
twig
Braces: {{ "{{" }}
Pi: {{ 3.1415926 }}
html
Braces: {{
Pi: 3.1415926
rust
let args = hashmap!{
"what".into() => ex("Hello"),
"who".into() => ex("World")
};
twig
Say: "{{ what + ", " + who }}!"
html
Say: "Hello, World!"
rust
let args = hashmap!{
"alpha".into() => ex(6isize),
"omega".into() => ex(7f64)
};
twig
The answer is {{ alpha * omega }}
html
The answer is 42
twig
Amount: {{ amount and ("" + amount + " pcs") or "-" }}
rust
assert_eq!("Amount: 6 pcs", incrust.render("tpl", hashmap!{ "amount".into() => ex(6isize) }).unwrap());
assert_eq!("Amount: -", incrust.render("tpl", hashmap!{ "amount".into() => ex(0isize) }).unwrap());
twig
String {% if "" %}has chars{% else %}is empty{% endif %}
It's {% if False %}false{% elif True %}true{% endif %}
html
String is empty
It's true
rust
let args = hashmap!{ "fruits".into() => ex(vec![ex("Orange"), ex("Apple"), ex("Banana")]) };
```twig
html
```
```twig
Visible {# partially #} paragraph
html
Visible paragraph
```
twig
Example: {% raw %}{{ mustaches }}{% endraw %}
html
Example: {{ mustaches }}
If you are looking for a template engine for your project, you may also look at these projects.
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.