[][src]Trait handlebars::HelperDef

pub trait HelperDef {
    fn call_inner<'reg: 'rc, 'rc>(
        &self,
        _: &Helper<'reg, 'rc>,
        _: &'reg Registry<'reg>,
        _: &'rc Context,
        _: &mut RenderContext<'reg, 'rc>
    ) -> Result<Option<ScopedJson<'reg, 'rc>>, RenderError> { ... }
fn call<'reg: 'rc, 'rc>(
        &self,
        h: &Helper<'reg, 'rc>,
        r: &'reg Registry<'reg>,
        ctx: &'rc Context,
        rc: &mut RenderContext<'reg, 'rc>,
        out: &mut dyn Output
    ) -> HelperResult { ... } }

Helper Definition

Implement HelperDef to create custom helpers. You can retrieve useful information from these arguments.

  • &Helper: current helper template information, contains name, params, hashes and nested template
  • &Registry: the global registry, you can find templates by name from registry
  • &Context: the whole data to render, in most case you can use data from Helper
  • &mut RenderContext: you can access data or modify variables (starts with @)/partials in render context, for example, @index of #each. See its document for detail.
  • &mut dyn Output: where you write output to

By default, you can use a bare function as a helper definition because we have supported unboxed_closure. If you have stateful or configurable helper, you can create a struct to implement HelperDef.

Define an inline helper

use handlebars::*;

fn upper(h: &Helper<'_, '_>, _: &Handlebars<'_>, _: &Context, rc: &mut RenderContext<'_, '_>, out: &mut Output)
    -> HelperResult {
   // get parameter from helper or throw an error
   let param = h.param(0).and_then(|v| v.value().as_str()).unwrap_or("");
   out.write(param.to_uppercase().as_ref())?;
   Ok(())
}

Define block helper

Block helper is like #if or #each which has a inner template and an optional inverse template (the template in else branch). You can access the inner template by helper.template() and helper.inverse(). In most cases you will just call render on it.

use handlebars::*;

fn dummy_block<'reg, 'rc>(
    h: &Helper<'reg, 'rc>,
    r: &'reg Handlebars<'reg>,
    ctx: &'rc Context,
    rc: &mut RenderContext<'reg, 'rc>,
    out: &mut dyn Output,
) -> HelperResult {
    h.template()
        .map(|t| t.render(r, ctx, rc, out))
        .unwrap_or(Ok(()))
}

Define helper function using macro

In most cases you just need some simple function to call from templates. We have a handlebars_helper! macro to simplify the job.

use handlebars::*;

handlebars_helper!(plus: |x: i64, y: i64| x + y);

let mut hbs = Handlebars::new();
hbs.register_helper("plus", Box::new(plus));

Provided methods

fn call_inner<'reg: 'rc, 'rc>(
    &self,
    _: &Helper<'reg, 'rc>,
    _: &'reg Registry<'reg>,
    _: &'rc Context,
    _: &mut RenderContext<'reg, 'rc>
) -> Result<Option<ScopedJson<'reg, 'rc>>, RenderError>

fn call<'reg: 'rc, 'rc>(
    &self,
    h: &Helper<'reg, 'rc>,
    r: &'reg Registry<'reg>,
    ctx: &'rc Context,
    rc: &mut RenderContext<'reg, 'rc>,
    out: &mut dyn Output
) -> HelperResult

Loading content...

Implementors

impl<F: for<'reg, 'rc> Fn(&Helper<'reg, 'rc>, &'reg Registry<'reg>, &'rc Context, &mut RenderContext<'reg, 'rc>, &mut dyn Output) -> HelperResult> HelperDef for F[src]

implement HelperDef for bare function so we can use function as helper

Loading content...