handlebars_sprig

Crates.iohandlebars_sprig
lib.rshandlebars_sprig
version0.3.0
sourcesrc
created_at2022-02-17 05:08:35.952933
updated_at2022-04-04 07:32:02.000953
descriptionhandlebar template function helpers
homepage
repository
max_upload_size
id533871
size37,108
Rajat Jindal (rajatjindal)

documentation

README

Handlebars-Sprig

Handlebars-Sprig is a port of the Sprig template functions to the Rust programming language, with support for Rust's Handlebars library.

It does not aim to be 100% compatible with Sprig. Go templates and Go idioms might not feel right in Handlebars and Rust. But Handlebars-Sprig does aim to be close enough that it feels familiar.

Handlebars-Sprig was originally developed for, and is curently used in the Bartholomew Micro-CMS.

Usage

In your rust code, you need to register the handlebars helper functions:

use handlebars::Handlebars;
use handlebars_sprig;

fn main() {
    // Get a handlebars instance
    let mut hbs = Handlebars::new();

    // THE IMPORTANT PART: Add the helpers
    handlebars_sprig::addhelpers(&mut hbs);

    // From this point, you can do whatever you want to do with the
    // handlebars instance. It will have all of the functions available
    // to the templates.
    let tpl = "{{ add 1 2 }}";

    // Example of running a template render.
    let empty_context: Vec<String> = vec![];
    println!("Template produced: {}", hbs.render_template(tpl, &empty_context).unwrap())
}

The above produces the following output:

$ cargo run --example basic
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `target/debug/examples/basic`
Template produced: 3
Commit count: 0

cargo fmt