pub trait DecoratorDef {
    fn call<'reg, 'rc>(
        &'reg self,
        d: &Decorator<'reg, 'rc>,
        r: &'reg Registry<'reg>,
        ctx: &'rc Context,
        rc: &mut RenderContext<'reg, 'rc>
    ) -> Result<(), RenderError>
        'reg: 'rc
; }
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"));

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
    rc.register_local_helper("distance", Box::new(new_helper));

Required Methods


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