pub struct HandlebarsConcat;
Expand description

Concat helper for handlebars-rust

§Registration

use handlebars::Handlebars;
use handlebars_concat::HandlebarsConcat;
use serde_json::json;

let mut h = Handlebars::new();
h.register_helper("concat", Box::new(HandlebarsConcat));

assert_eq!(h.render_template(r#"{{concat item1 item2}}"#, &json!({"item1": "Value 1", "item2": "Value 2"})).expect("Render error"), "Value 1,Value 2");
assert_eq!(h.render_template(r#"{{concat this separator=", "}}"#, &json!({"item1": "Value 1", "item2": "Value 2"})).expect("Render error"), "item1, item2");
assert_eq!(h.render_template(r#"{{#concat this separator=", "}}{{this}}{{/concat}}"#, &json!({"item1": "Value 1", "item2": "Value 2"})).expect("Render error"), "Value 1, Value 2");
assert_eq!(h.render_template(r#"{{#concat "Form" this separator="" render_all=true}}<{{#if tag}}{{tag}}{{else}}{{this}}{{/if}}/>{{/concat}}"#, &json!({"key0":{"tag":"Input"},"key1":{"tag":"Select"},"key2":{"tag":"Button"}})).expect("Render error"), "<Form/><Input/><Select/><Button/>");

§Behavior

The helper is looking for multiple arguments of type string, array or object. Arguments are being added to an output buffer and returned altogether as string.

The helper has few parameters modifying the behavior slightly. For example distinct=true eliminates duplicate values from the output buffer, while quotes=true in combination with single_quote=true wraps the values in quotation marks.

§String

String arguments are added directly to the output buffer. As of 0.1.3 strings could be handled in one of two ways:

  1. By default strings are added to the output buffer without modification (other than the quotation mark modifiers).
  2. If you add a block template and use the render_all parameter, strings will be passed as {{this}} to the block template.

The block template rendering is disabled by default for backward compatibility.

§Array

Array arguments are iterated and added as individual strings to the output buffer. As of 0.1.3 arrays could be handled in one of two ways:

  1. By default array values are added as individual strings to the output buffer without modification (other than the quotation mark modifiers).
  2. If you add a block template and use the render_all parameter, array values are passed as {{this}} to the block template.

The block template rendering is disabled by default for backward compatibility.

§Object

Object arguments could be handled two different ways:

  1. By default only the object keys are being used and the values are ignored.
  2. If you add a block template the helper will use it to render the object value and concatenate it as string to the output buffer.

Object rendering results are subject to distinct, quotes and single_quote modifier parameters, just like strings and arrays.

§Hash parameters

  • separator: Set specific string to join elements with. Default is “,”
  • distinct: Eliminate duplicates upon adding to output buffer
  • quotes: Wrap each value in double quotation marks
  • single_quote: Modifier of quotes to switch to single quotation mark instead
  • render_all: Render all values using the block template, not just object values

§Example usage:

Example with string literals:

{{concat "One" "Two" separator=", "}}

Result: One, Two


{{concat "One" "Two" separator=", " quotes=true}}

Result: "One", "Two"


Where s is "One", arr is ["One", "Two"] and obj is {"Three":3}

{{concat s arr obj separator=", " distinct=true}}

Result: One, Two, Three


Where s is "One", arr is ["One", "Two"] and obj is {"key0":{"label":"Two"},"key1":{"label":"Three"},"key2":{"label":"Four"}}:

{{#concat s arr obj separator=", " distinct=true}}{{label}}{{/concat}}

Result: One, Two, Three, Four


Where s is "One", arr is ["One", "Two"] and obj is {"key0":{"label":"Two"},"key1":{"label":"Three"},"key2":{"label":"Four"}}

{{#concat s arr obj separator=", " distinct=true render_all=true}}<{{#if label}}{{label}}{{else}}{{this}}{{/if}}/>{{/concat}}

Result: <One/>, <Two/>, <Three/>, <Four/>


Trait Implementations§

source§

impl Clone for HandlebarsConcat

source§

fn clone(&self) -> HandlebarsConcat

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl HelperDef for HandlebarsConcat

source§

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

A complex version of helper interface. Read more
source§

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

A simplified api to define helper Read more
source§

impl Copy for HandlebarsConcat

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.