pub trait DecoratorDef {
    // Required method
    fn call<'reg: 'rc, 'rc>(
        &'reg self,
        d: &Decorator<'rc>,
        r: &'reg Registry<'reg>,
        ctx: &'rc Context,
        rc: &mut RenderContext<'reg, 'rc>
    ) -> Result<(), RenderError>;
}
Expand description

Decorator Definition

Implement this trait to define your own decorators. Currently decorator shares same definition with helper.

In handlebars, it is recommended to use decorator to change context data and update helper definition.

§Updating context data

In decorator, you can change some context data you are about to render.

use handlebars::*;

fn update_data<'reg: 'rc, 'rc>(_: &Decorator, _: &Handlebars, ctx: &Context, rc: &mut RenderContext)
        -> Result<(), RenderError> {
    // modify json object
    let mut new_ctx = ctx.clone();
    {
        let mut data = new_ctx.data_mut();
        if let Some(ref mut m) = data.as_object_mut() {
            m.insert("hello".to_string(), to_json("world"));
        }
    }
    rc.set_context(new_ctx);
    Ok(())
}

§Define local helper

You can override behavior of a helper from position of decorator to the end of template.

use handlebars::*;

fn override_helper(_: &Decorator, _: &Handlebars, _: &Context, rc: &mut RenderContext)
        -> Result<(), RenderError> {
    let new_helper = |h: &Helper, _: &Handlebars, _: &Context, rc: &mut RenderContext, out: &mut dyn Output|
            -> Result<(), RenderError> {
        // your helper logic
        Ok(())
    };
    rc.register_local_helper("distance", Box::new(new_helper));
    Ok(())
}

Required Methods§

source

fn call<'reg: 'rc, 'rc>( &'reg self, d: &Decorator<'rc>, r: &'reg Registry<'reg>, ctx: &'rc Context, rc: &mut RenderContext<'reg, 'rc> ) -> Result<(), RenderError>

Implementors§

source§

impl<F: for<'reg, 'rc> Fn(&Decorator<'rc>, &'reg Registry<'reg>, &'rc Context, &mut RenderContext<'reg, 'rc>) -> Result<(), RenderError>> DecoratorDef for F

Implement DecoratorDef for bare function so we can use function as decorator