bracket/helper/
unless.rs

1//! Block helper for negated conditional.
2use crate::{
3    helper::{Helper, HelperValue},
4    parser::ast::Node,
5    render::{Context, Render},
6};
7
8/// Render an inner block when the conditional is **not** truthy.
9///
10/// If any `else` or `else if` conditionals are present they will
11/// be rendered when necessary.
12pub struct Unless;
13
14impl Helper for Unless {
15    fn call<'render, 'call>(
16        &self,
17        rc: &mut Render<'render>,
18        ctx: &Context<'call>,
19        template: Option<&'render Node<'render>>,
20    ) -> HelperValue {
21        ctx.arity(1..1)?;
22
23        if let Some(template) = template {
24            if !ctx.is_truthy(ctx.get(0).unwrap()) {
25                rc.template(template)?;
26            } else if let Some(node) = rc.inverse(template)? {
27                rc.template(node)?;
28            }
29        }
30
31        Ok(None)
32    }
33}