Struct handlebars::Handlebars[][src]

pub struct Handlebars<'reg> { /* fields omitted */ }
Expand description

The single entry point of your Handlebars templates

It maintains compiled templates and registered helpers.

Implementations

impl<'reg> Registry<'reg>[src]

pub fn new() -> Registry<'reg>[src]

pub fn set_strict_mode(&mut self, enabled: bool)[src]

Enable or disable handlebars strict mode

By default, handlebars renders empty string for value that undefined or never exists. Since rust is a static type language, we offer strict mode in handlebars-rust. In strict mode, if you were to render a value that doesn’t exist, a RenderError will be raised.

pub fn strict_mode(&self) -> bool[src]

Return strict mode state, default is false.

By default, handlebars renders empty string for value that undefined or never exists. Since rust is a static type language, we offer strict mode in handlebars-rust. In strict mode, if you were access a value that doesn’t exist, a RenderError will be raised.

pub fn dev_mode(&self) -> bool[src]

Return dev mode state, default is false

With dev mode turned on, handlebars enables a set of development firendly features, that may affect its performance.

pub fn set_dev_mode(&mut self, enabled: bool)[src]

Enable or disable dev mode

With dev mode turned on, handlebars enables a set of development firendly features, that may affect its performance.

pub fn register_template(&mut self, name: &str, tpl: Template)[src]

Register a Template

This is infallible since the template has already been parsed and insert cannot fail. If there is an existing template with this name it will be overwritten.

pub fn register_template_string<S>(
    &mut self,
    name: &str,
    tpl_str: S
) -> Result<(), TemplateError> where
    S: AsRef<str>, 
[src]

Register a template string

Returns TemplateError if there is syntax error on parsing the template.

pub fn register_partial<S>(
    &mut self,
    name: &str,
    partial_str: S
) -> Result<(), TemplateError> where
    S: AsRef<str>, 
[src]

Register a partial string

A named partial will be added to the registry. It will overwrite template with same name. Currently a registered partial is just identical to a template.

pub fn register_template_file<P>(
    &mut self,
    name: &str,
    tpl_path: P
) -> Result<(), TemplateError> where
    P: AsRef<Path>, 
[src]

Register a template from a path

pub fn register_templates_directory<P>(
    &mut self,
    tpl_extension: &'static str,
    dir_path: P
) -> Result<(), TemplateError> where
    P: AsRef<Path>, 
[src]

This is supported on crate feature dir_source only.

Register templates from a directory

  • tpl_extension: the template file extension
  • dir_path: the path of directory

Hidden files and tempfile (starts with #) will be ignored. All registered will use their relative name as template name. For example, when dir_path is templates/ and tpl_extension is .hbs, the file templates/some/path/file.hbs will be registered as some/path/file.

This method is not available by default. You will need to enable the dir_source feature to use it.

pub fn unregister_template(&mut self, name: &str)[src]

Remove a template from the registry

pub fn register_helper(
    &mut self,
    name: &str,
    def: Box<dyn HelperDef + Send + Sync + 'reg>
)
[src]

Register a helper

pub fn register_script_helper(
    &mut self,
    name: &str,
    script: &str
) -> Result<(), ScriptError>
[src]

This is supported on crate feature script_helper only.

Register a rhai script as handlebars helper

Currently only simple helpers are supported. You can do computation or string formatting with rhai script.

Helper parameters and hash are available in rhai script as array params and map hash. Example script:

{{percent 0.34 label="%"}}
// percent.rhai
let value = params[0];
let label = hash["label"];

(value * 100).to_string() + label

pub fn register_script_helper_file<P>(
    &mut self,
    name: &str,
    script_path: P
) -> Result<(), ScriptError> where
    P: AsRef<Path>, 
[src]

This is supported on crate feature script_helper only.

Register a rhai script from file

pub fn register_decorator(
    &mut self,
    name: &str,
    def: Box<dyn DecoratorDef + Send + Sync + 'reg>
)
[src]

Register a decorator

pub fn register_escape_fn<F: 'static + Fn(&str) -> String + Send + Sync>(
    &mut self,
    escape_fn: F
)
[src]

Register a new escape fn to be used from now on by this registry.

pub fn unregister_escape_fn(&mut self)[src]

Restore the default escape fn.

pub fn get_escape_fn(&self) -> &dyn Fn(&str) -> String[src]

Get a reference to the current escape fn.

pub fn has_template(&self, name: &str) -> bool[src]

Return true if a template is registered for the given name

pub fn get_template(&self, name: &str) -> Option<&Template>[src]

Return a registered template,

pub fn get_templates(&self) -> &HashMap<String, Template>[src]

Return all templates registered

pub fn clear_templates(&mut self)[src]

Unregister all templates

pub fn render<T>(&self, name: &str, data: &T) -> Result<String, RenderError> where
    T: Serialize
[src]

Render a registered template with some data into a string

  • name is the template name you registered previously
  • data is the data that implements serde::Serialize

Returns rendered string or a struct with error information

pub fn render_with_context(
    &self,
    name: &str,
    ctx: &Context
) -> Result<String, RenderError>
[src]

Render a registered template with reused context

pub fn render_to_write<T, W>(
    &self,
    name: &str,
    data: &T,
    writer: W
) -> Result<(), RenderError> where
    T: Serialize,
    W: Write
[src]

Render a registered template and write some data to the std::io::Write

pub fn render_template<T>(
    &self,
    template_string: &str,
    data: &T
) -> Result<String, RenderError> where
    T: Serialize
[src]

Render a template string using current registry without registering it

pub fn render_template_with_context(
    &self,
    template_string: &str,
    ctx: &Context
) -> Result<String, RenderError>
[src]

Render a template string using reused context data

pub fn render_template_to_write<T, W>(
    &self,
    template_string: &str,
    data: &T,
    writer: W
) -> Result<(), RenderError> where
    T: Serialize,
    W: Write
[src]

Render a template string using current registry without registering it

Trait Implementations

impl<'reg> Clone for Registry<'reg>[src]

fn clone(&self) -> Registry<'reg>[src]

Returns a copy of the value. Read more

fn clone_from(&mut self, source: &Self)1.0.0[src]

Performs copy-assignment from source. Read more

impl<'reg> Debug for Registry<'reg>[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>[src]

Formats the value using the given formatter. Read more

impl<'reg> Default for Registry<'reg>[src]

fn default() -> Self[src]

Returns the “default value” for a type. Read more

Auto Trait Implementations

impl<'reg> !RefUnwindSafe for Registry<'reg>

impl<'reg> Send for Registry<'reg>

impl<'reg> Sync for Registry<'reg>

impl<'reg> Unpin for Registry<'reg>

impl<'reg> !UnwindSafe for Registry<'reg>

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

pub fn to_owned(&self) -> T[src]

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

pub fn clone_into(&self, target: &mut T)[src]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

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

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

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

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

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

Performs the conversion.