2 releases

0.0.2 Oct 1, 2023
0.0.1 May 1, 2023

#247 in Template engine

26 downloads per month
Used in 3 crates (via wakflo-core)

Apache-2.0 OR MIT

105KB
2K SLoC

Rion Templating Engine

based on https://github.com/bheisler/TinyTemplate


lib.rs:

Rion

Rion is a minimal templating library based on TinyTemplate originally designed for use in Criterion.rs. It deliberately does not provide all of the features of a full-power template engine, but in return it provides a simple API, clear templating syntax, decent performance and very few dependencies.

Features

The most important features are as follows (see the syntax module for full details on the template syntax):

  • Rendering values - { myvalue }
  • Conditionals - {{ if foo }}Foo is true{{ else }}Foo is false{{ endif }}
  • Loops - {{ for value in row }}{value}{{ endfor }}
  • Customizable value formatters { value | my_formatter }
  • Macros {{ call my_template with foo }}

Restrictions

Rion was designed with the assumption that the templates are available as static strings, either using string literals or the include_str! macro. Thus, it borrows &str slices from the template text itself and uses them during the rendering process. Although it is possible to use Rion with template strings loaded at runtime, this is not recommended.

Additionally, Rion can only render templates into Strings. If you need to render a template directly to a socket or file, Rion may not be right for you.

Example

#[macro_use]
extern crate serde_derive;
extern crate rion;

use rion::Rion;
use std::error::Error;

#[derive(Serialize)]
struct Context {
    name: String,
}

static TEMPLATE : &'static str = "Hello {name}!";

pub fn main() -> Result<(), Box<dyn Error>> {
    let mut tt = Rion::new();
    tt.add_template("hello", TEMPLATE)?;

    let context = Context {
        name: "World".to_string(),
    };

    let rendered = tt.render("hello", &context)?;
    println!("{}", rendered);

    Ok(())
}

Dependencies

~3.5–5MB
~99K SLoC