use super::handlebars_ext::HandlebarsExt;
use handlebars::{HelperDef, Renderable};
pub const IF_EQUALS_HELPER: &str = "if_equals";
pub const IF_NOT_EQUALS_HELPER: &str = "if_not_equals";
pub struct IfEqualsHelper;
impl HelperDef for IfEqualsHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &handlebars::Helper<'reg, 'rc>,
handle: &'reg handlebars::Handlebars<'reg>,
ctx: &'rc handlebars::Context,
render_ctx: &mut handlebars::RenderContext<'reg, 'rc>,
out: &mut dyn handlebars::Output,
) -> handlebars::HelperResult {
h.ensure_arguments_count_min(2, IF_EQUALS_HELPER)?;
let value = h.get_param_as_json_or_fail(0, IF_EQUALS_HELPER)?;
let is_value_found = h.params().iter().skip(1).any(|p| p.value() == value);
let temp = if is_value_found { h.template() } else { h.inverse() };
match temp {
Some(t) => t.render(handle, ctx, render_ctx, out),
None => Ok(()),
}
}
}
pub struct IfNotEqualsHelper;
impl HelperDef for IfNotEqualsHelper {
fn call<'reg: 'rc, 'rc>(
&self,
h: &handlebars::Helper<'reg, 'rc>,
handle: &'reg handlebars::Handlebars<'reg>,
ctx: &'rc handlebars::Context,
render_ctx: &mut handlebars::RenderContext<'reg, 'rc>,
out: &mut dyn handlebars::Output,
) -> handlebars::HelperResult {
h.ensure_arguments_count_min(2, IF_NOT_EQUALS_HELPER)?;
let value = h.get_param_as_json_or_fail(0, IF_NOT_EQUALS_HELPER)?;
let is_value_found = h.params().iter().skip(1).any(|p| p.value() == value);
let temp = if !is_value_found { h.template() } else { h.inverse() };
match temp {
Some(t) => t.render(handle, ctx, render_ctx, out),
None => Ok(()),
}
}
}