3个版本 (破坏性)
| 0.3.0 | 2022年4月4日 | 
|---|---|
| 0.2.0 | 2022年2月21日 | 
| 0.1.0 | 2022年2月17日 | 
#416 in 模板引擎
12KB
163 行
Handlebars-Sprig
Handlebars-Sprig是将Sprig模板函数移植到Rust编程语言的实现,同时支持Rust的Handlebars库。
它并不旨在与Sprig完全兼容。Go模板和Go语法可能在Handlebars和Rust中感觉不太合适。但Handlebars-Sprig的目标是足够接近,使其感觉熟悉。
Handlebars-Sprig最初是为Bartholomew Micro-CMS开发的,并且目前正在该系统中使用。
使用方法
在您的Rust代码中,您需要注册handlebars助手函数
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())
}
上面的代码产生以下输出
$ cargo run --example basic
    Finished dev [unoptimized + debuginfo] target(s) in 0.05s
     Running `target/debug/examples/basic`
Template produced: 3
依赖项
~8–18MB
~227K SLoC