From a8f48751a4704f2d6cb8b0f1b9662cf9b78c53d5 Mon Sep 17 00:00:00 2001 From: Ning Sun Date: Thu, 7 Jul 2022 09:49:52 +0800 Subject: [PATCH] chore: update example for helper macro --- examples/helper_macro.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/examples/helper_macro.rs b/examples/helper_macro.rs index 6dda85678..4b3273337 100644 --- a/examples/helper_macro.rs +++ b/examples/helper_macro.rs @@ -1,5 +1,7 @@ use std::error::Error; +use handlebars::JsonRender; + use handlebars::{handlebars_helper, Handlebars}; use time::format_description::parse; use time::OffsetDateTime; @@ -11,6 +13,11 @@ handlebars_helper!(date: |dt: OffsetDateTime| dt.format(&parse("[year]-[month]-[ // a helper returns number of provided parameters handlebars_helper!(nargs: |*args| args.len()); +// a helper joins all values, using both hash and parameters +handlebars_helper!(join: |{sep:str=","}, *args| + args.iter().map(|a| a.render()).collect::>().join(sep) +); + // a helper provides format handlebars_helper!(date2: |dt: OffsetDateTime, {fmt:str = "[year]-[month]-[day]"}| dt.format(&parse(fmt).unwrap()).unwrap() @@ -23,6 +30,7 @@ fn main() -> Result<(), Box> { handlebars.register_helper("date", Box::new(date)); handlebars.register_helper("date2", Box::new(date2)); handlebars.register_helper("nargs", Box::new(nargs)); + handlebars.register_helper("join", Box::new(join)); let data = OffsetDateTime::now_utc(); @@ -35,5 +43,10 @@ fn main() -> Result<(), Box> { println!("{}", handlebars.render_template("{{nargs 1 2 3 4}}", &())?); + println!( + "{}", + handlebars.render_template("{{join 1 2 3 4 sep=\"|\" }}", &())? + ); + Ok(()) }