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
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.
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.
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.
Enable or disable dev mode
With dev mode turned on, handlebars enables a set of development firendly features, that may affect its performance.
Note that you have to enable dev mode before adding templates to the registry. Otherwise it won’t take effect at all.
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.
Dev mode doesn’t apply for pre-compiled template because it’s lifecycle is not managed by the registry.
pub fn register_template_string<S>(
&mut self,
name: &str,
tpl_str: S
) -> Result<(), TemplateError> where
S: AsRef<str>,
pub fn register_template_string<S>(
&mut self,
name: &str,
tpl_str: S
) -> Result<(), TemplateError> where
S: AsRef<str>,
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>,
pub fn register_partial<S>(
&mut self,
name: &str,
partial_str: S
) -> Result<(), TemplateError> where
S: AsRef<str>,
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>,
pub fn register_template_file<P>(
&mut self,
name: &str,
tpl_path: P
) -> Result<(), TemplateError> where
P: AsRef<Path>,
Register a template from a path on file system
If dev mode is enabled, the registry will keep reading the template file from file system everytime it’s visited.
pub fn register_templates_directory<P>(
&mut self,
tpl_extension: &'static str,
dir_path: P
) -> Result<(), TemplateError> where
P: AsRef<Path>,
This is supported on crate feature dir_source
only.
pub fn register_templates_directory<P>(
&mut self,
tpl_extension: &'static str,
dir_path: P
) -> Result<(), TemplateError> where
P: AsRef<Path>,
dir_source
only.Register templates from a directory
tpl_extension
: the template file extensiondir_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.
When dev_mode enabled, like register_template_file
, templates is reloaded
from file system everytime it’s visied.
Remove a template from the registry
Register a helper
This is supported on crate feature script_helper
only.
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>,
This is supported on crate feature script_helper
only.
pub fn register_script_helper_file<P>(
&mut self,
name: &str,
script_path: P
) -> Result<(), ScriptError> where
P: AsRef<Path>,
script_helper
only.Register a rhai script from file
When dev mode is enable, script file is reloaded from original file everytime it is called.
This is supported on crate feature script_helper
only.
script_helper
only.Borrow a read-only reference to current rhai engine
This is supported on crate feature script_helper
only.
script_helper
only.Set a custom rhai engine for the registry.
Note that you need to set custom engine before adding scripts.
pub fn register_decorator(
&mut self,
name: &str,
def: Box<dyn DecoratorDef + Send + Sync + 'reg>
)
pub fn register_decorator(
&mut self,
name: &str,
def: Box<dyn DecoratorDef + Send + Sync + 'reg>
)
Register a decorator
Register a new escape fn to be used from now on by this registry.
Restore the default escape fn.
Get a reference to the current escape fn.
Return true
if a template is registered for the given name
Return a registered template,
Return all templates registered
Note that in dev mode, the template returned from this method may not reflect its latest state. This method doesn’t try to reload templates from its source.
Unregister all templates
Render a registered template with some data into a string
name
is the template name you registered previouslydata
is the data that implementsserde::Serialize
Returns rendered string or a struct with error information
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,
pub fn render_to_write<T, W>(
&self,
name: &str,
data: &T,
writer: W
) -> Result<(), RenderError> where
T: Serialize,
W: Write,
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,
pub fn render_template<T>(
&self,
template_string: &str,
data: &T
) -> Result<String, RenderError> where
T: Serialize,
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>
pub fn render_template_with_context(
&self,
template_string: &str,
ctx: &Context
) -> Result<String, RenderError>
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,
pub fn render_template_to_write<T, W>(
&self,
template_string: &str,
data: &T,
writer: W
) -> Result<(), RenderError> where
T: Serialize,
W: Write,
Render a template string using current registry without registering it
Trait Implementations
Auto Trait Implementations
impl<'reg> !RefUnwindSafe for Registry<'reg>
impl<'reg> !UnwindSafe for Registry<'reg>
Blanket Implementations
Mutably borrows from an owned value. Read more