Debian Codegraph @

tests/helper_function_lifetime.rs

raw symbols SCIP
1 use handlebars::*;
2
3 fn ifcond<'reg, 'rc>(
4 h: &Helper<'rc>,
5 handle: &'reg Handlebars,
6 ctx: &'rc Context,
7 render_ctx: &mut RenderContext<'reg, 'rc>,
8 out: &mut dyn Output,
9 ) -> Result<(), RenderError> {
10 let cond = h.param(0).and_then(|ref v| v.value().as_bool()).ok_or(
11 RenderErrorReason::ParamTypeMismatchForName("ifcond", "0".to_owned(), "bool".to_owned()),
12 )? as bool;
13 let temp = if cond { h.template() } else { h.inverse() };
14 match temp {
15 Some(t) => t.render(handle, ctx, render_ctx, out),
16 None => Ok(()),
17 }
18 }
19
20 #[test]
21 fn test_helper() {
22 let mut handlebars = Handlebars::new();
23
24 // register some custom helpers
25 handlebars.register_helper("ifcond", Box::new(ifcond));
26
27 // make data and render it
28 let data = true;
29 assert_eq!(
30 "yes",
31 handlebars
32 .render_template("{{#ifcond this}}yes{{/ifcond}}", &data)
33 .unwrap()
34 );
35 }