{% Incrust %}
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.
Unstable
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.
Installation
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:
extern crate maplit;
extern crate incrust;
use ex;
Though Incrust has smart loaders, it may be used just as advanced formatter to render directly from string template
incrust.render_text.unwrap;
// or with prepared template
let hello = incrust.parse;
let args = hashmap!;
incrust.render_parsed.unwrap;
Syntax examples
Filters
let args = hashmap!;
<h1>{{ title | escape }}</h1>
<Cats & Dogs>
Literals
Braces: {{ "{{" }}
Pi: {{ 3.1415926 }}
Braces: {{
Pi: 3.1415926
Expressions
let args = hashmap!;
Say: "{{ what + ", " + who }}!"
Say: "Hello, World!"
let args = hashmap!;
The answer is {{ alpha * omega }}
The answer is 42
Lazy boolean evaluation
Amount: {{ amount and ("" + amount + " pcs") or "-" }}
assert_eq!;
assert_eq!;
Conditional statements
String {% if "" %}has chars{% else %}is empty{% endif %}
It's {% if False %}false{% elif True %}true{% endif %}
String is empty
It's true
For-Loop statements
let args = hashmap!;
<ul>{% for fruit in fruits %}<li>{{ index }}. {{ fruit | e }}</li>{% endfor %}</ul>
1. Orange 2. Apple 3. Banana
Comments
<p>Visible {# partially #} paragraph</p>
Visible paragraph
Escaping
Example: {% raw %}{{ mustaches }}{% endraw %}
Example: {{ mustaches }}
Alternatives
If you are looking for a template engine for your project, you may also look at these projects.
With a similar to Incrust syntax
- cobalt-org/liquid-rust Liquid templating for Rust
- colin-kiegel/twig-rust Rust port of the twig-php template library
- Nercury/twig-rs The Twig templating engine for Rust (work in progress)
- Keats/tera A template engine for Rust
Others
- sunng87/handlebars-rust Rust templating with Handlebars
- nickel-org/rust-mustache Mustache template library for rust
License
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.
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.