Skip to content

Commit

Permalink
test: add example for testing value in helper macro #516
Browse files Browse the repository at this point in the history
  • Loading branch information
sunng87 committed Jul 14, 2022
1 parent bfddca7 commit ac5bd3c
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions examples/helper_macro.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use std::error::Error;

use handlebars::JsonRender;

use handlebars::{handlebars_helper, Handlebars};
use handlebars::{handlebars_helper, Handlebars, JsonRender};
use serde_json::{json, Value};
use time::format_description::parse;
use time::OffsetDateTime;

Expand All @@ -18,6 +17,8 @@ handlebars_helper!(join: |{sep:str=","}, *args|
args.iter().map(|a| a.render()).collect::<Vec<String>>().join(sep)
);

handlebars_helper!(isdefined: |v: Value| !v.is_null());

// a helper provides format
handlebars_helper!(date2: |dt: OffsetDateTime, {fmt:str = "[year]-[month]-[day]"}|
dt.format(&parse(fmt).unwrap()).unwrap()
Expand All @@ -31,6 +32,7 @@ fn main() -> Result<(), Box<dyn Error>> {
handlebars.register_helper("date2", Box::new(date2));
handlebars.register_helper("nargs", Box::new(nargs));
handlebars.register_helper("join", Box::new(join));
handlebars.register_helper("isdefined", Box::new(isdefined));

let data = OffsetDateTime::now_utc();

Expand All @@ -48,5 +50,14 @@ fn main() -> Result<(), Box<dyn Error>> {
handlebars.render_template("{{join 1 2 3 4 sep=\"|\" }}", &())?
);

println!(
"{}",
handlebars.render_template(
r#"{{isdefined a}} {{isdefined b}}
{{#if (isdefined a)}}a{{/if}} {{#if (isdefined b)}}b{{/if}}"#,
&json!({"a": 1})
)?
);

Ok(())
}

0 comments on commit ac5bd3c

Please sign in to comment.