[−][src]Trait handlebars::HelperDef
Helper Definition
Implement HelperDef
to create custom helper. 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 fromHelper
&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 Output
: where you write output to
By default, you can use bare function as 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 case you will just call render
on it.
use handlebars::*; fn dummy_block<'reg, 'rc>( h: &Helper<'reg, 'rc>, r: &'reg Handlebars, ctx: &Context, rc: &mut RenderContext<'reg>, out: &mut Output, ) -> HelperResult { h.template() .map(|t| t.render(r, ctx, rc, out)) .unwrap_or(Ok(())) }
Define helper function using macro
In most case you just need some simple function to call from template. We have 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,
_: &'rc Context,
_: &mut RenderContext<'reg>
) -> Result<Option<ScopedJson<'reg, 'rc>>, RenderError>
&self,
_: &Helper<'reg, 'rc>,
_: &'reg Registry,
_: &'rc Context,
_: &mut RenderContext<'reg>
) -> Result<Option<ScopedJson<'reg, 'rc>>, RenderError>
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
r: &'reg Registry,
ctx: &'rc Context,
rc: &mut RenderContext<'reg>,
out: &mut dyn Output
) -> HelperResult
&self,
h: &Helper<'reg, 'rc>,
r: &'reg Registry,
ctx: &'rc Context,
rc: &mut RenderContext<'reg>,
out: &mut dyn Output
) -> HelperResult
Implementors
impl<F: Send + Sync + for<'reg, 'rc> Fn(&Helper<'reg, 'rc>, &'reg Registry, &'rc Context, &mut RenderContext<'reg>, &mut dyn Output) -> HelperResult> HelperDef for F
[src]
implement HelperDef for bare function so we can use function as helper
fn call<'reg: 'rc, 'rc>(
&self,
h: &Helper<'reg, 'rc>,
r: &'reg Registry,
ctx: &'rc Context,
rc: &mut RenderContext<'reg>,
out: &mut dyn Output
) -> HelperResult
[src]
&self,
h: &Helper<'reg, 'rc>,
r: &'reg Registry,
ctx: &'rc Context,
rc: &mut RenderContext<'reg>,
out: &mut dyn Output
) -> HelperResult