bracket/helper/
with.rs

1//! Block helper that sets the scope.
2use crate::{
3    helper::{Helper, HelperValue},
4    parser::ast::Node,
5    render::{Context, Render, Scope},
6};
7
8use serde_json::Value;
9
10/// Set the scope for a block to the target argument.
11pub struct With;
12
13impl Helper for With {
14    fn call<'render, 'call>(
15        &self,
16        rc: &mut Render<'render>,
17        ctx: &Context<'call>,
18        template: Option<&'render Node<'render>>,
19    ) -> HelperValue {
20        ctx.arity(1..1)?;
21
22        if let Some(arg) = ctx.get(0) {
23            let is_null = if let Value::Null = arg { true } else { false };
24            if !is_null {
25                if let Some(template) = template {
26                    rc.push_scope(Scope::new());
27                    if let Some(ref mut scope) = rc.scope_mut() {
28                        scope.set_base_value(ctx.get(0).cloned().unwrap());
29                    }
30                    rc.template(template)?;
31                    rc.pop_scope();
32                }
33            }
34        }
35
36        Ok(None)
37    }
38}