Trait handlebars::DecoratorDef [] [src]

pub trait DecoratorDef: Send + Sync {
    fn call(
        &self,
        d: &Directive,
        r: &Registry,
        rc: &mut RenderContext
    ) -> Result<(), RenderError>; }

Decorator Definition

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

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

Updating context data

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

use handlebars::*;

fn update_data(_: &Decorator, _: &Handlebars, rc: &mut RenderContext)
        -> Result<(), RenderError> {
    // modify json object
    let mut data = rc.context_mut().data_mut();
    if let Some(ref mut m) = data.as_object_mut() {
        m.insert("hello".to_string(), to_json(&"world".to_owned()));
    }
    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, rc: &mut RenderContext)
        -> Result<(), RenderError> {
    let new_helper = |h: &Helper, _: &Handlebars, rc: &mut RenderContext|
            -> Result<(), RenderError> {
        // your helper logic
        Ok(())
    };
    rc.register_local_helper("distance", Box::new(new_helper));
    Ok(())
}

Required Methods

Implementors