Struct handlebars::Handlebars
source · pub struct Handlebars { /* private fields */ }
Expand description
The single entry point of your Handlebars templates
It maintains compiled templates and registered helpers.
Implementations
sourceimpl Registry
impl Registry
pub fn new() -> Registry
sourcepub fn source_map_enabled(&mut self, enable: bool)
pub fn source_map_enabled(&mut self, enable: bool)
Enable handlebars template source map
Source map provides line/col reporting on error. It uses slightly more memory to maintain the data.
Default is true.
sourcepub fn set_strict_mode(&mut self, enable: bool)
pub fn set_strict_mode(&mut self, enable: bool)
Enable 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 access a value that doesn’t exist, a
RenderError
will be raised.
sourcepub fn strict_mode(&self) -> bool
pub fn strict_mode(&self) -> bool
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.
sourcepub 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 template.
sourcepub 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 registered partial is just identical to template.
sourcepub fn register_template_file<P>(
&mut self,
name: &str,
tpl_path: P
) -> Result<(), TemplateFileError>where
P: AsRef<Path>,
pub fn register_template_file<P>(
&mut self,
name: &str,
tpl_path: P
) -> Result<(), TemplateFileError>where
P: AsRef<Path>,
Register a template from a path
sourcepub fn register_templates_directory<P>(
&mut self,
tpl_extension: &'static str,
dir_path: P
) -> Result<(), TemplateFileError>where
P: AsRef<Path>,
pub fn register_templates_directory<P>(
&mut self,
tpl_extension: &'static str,
dir_path: P
) -> Result<(), TemplateFileError>where
P: AsRef<Path>,
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 registerd as some/path/file
.
sourcepub fn register_template_source(
&mut self,
name: &str,
tpl_source: &mut dyn Read
) -> Result<(), TemplateFileError>
pub fn register_template_source(
&mut self,
name: &str,
tpl_source: &mut dyn Read
) -> Result<(), TemplateFileError>
Register a template from std::io::Read
source
sourcepub fn unregister_template(&mut self, name: &str)
pub fn unregister_template(&mut self, name: &str)
remove a template from the registry
sourcepub fn register_helper(
&mut self,
name: &str,
def: Box<dyn HelperDef + 'static>
) -> Option<Box<dyn HelperDef + 'static>>
pub fn register_helper(
&mut self,
name: &str,
def: Box<dyn HelperDef + 'static>
) -> Option<Box<dyn HelperDef + 'static>>
register a helper
sourcepub fn register_decorator(
&mut self,
name: &str,
def: Box<dyn DirectiveDef + 'static>
) -> Option<Box<dyn DirectiveDef + 'static>>
pub fn register_decorator(
&mut self,
name: &str,
def: Box<dyn DirectiveDef + 'static>
) -> Option<Box<dyn DirectiveDef + 'static>>
register a decorator
sourcepub fn register_escape_fn<F: 'static + Fn(&str) -> String + Send + Sync>(
&mut self,
escape_fn: F
)
pub fn register_escape_fn<F: 'static + Fn(&str) -> String + Send + Sync>(
&mut self,
escape_fn: F
)
Register a new escape fn to be used from now on by this registry.
sourcepub fn unregister_escape_fn(&mut self)
pub fn unregister_escape_fn(&mut self)
Restore the default escape fn.
sourcepub fn get_escape_fn(&self) -> &dyn Fn(&str) -> String
pub fn get_escape_fn(&self) -> &dyn Fn(&str) -> String
Get a reference to the current escape fn.
sourcepub fn has_template(&self, name: &str) -> bool
pub fn has_template(&self, name: &str) -> bool
Return true
if a template is registered for the given name
sourcepub fn get_template(&self, name: &str) -> Option<&Template>
pub fn get_template(&self, name: &str) -> Option<&Template>
Return a registered template,
sourcepub fn get_helper(&self, name: &str) -> Option<&Box<dyn HelperDef + 'static>>
pub fn get_helper(&self, name: &str) -> Option<&Box<dyn HelperDef + 'static>>
Return a registered helper
sourcepub fn get_decorator(
&self,
name: &str
) -> Option<&Box<dyn DirectiveDef + 'static>>
pub fn get_decorator(
&self,
name: &str
) -> Option<&Box<dyn DirectiveDef + 'static>>
Return a registered directive, aka decorator
sourcepub fn get_templates(&self) -> &HashMap<String, Template>
pub fn get_templates(&self) -> &HashMap<String, Template>
Return all templates registered
sourcepub fn clear_templates(&mut self)
pub fn clear_templates(&mut self)
Unregister all templates
sourcepub fn render<T>(&self, name: &str, data: &T) -> Result<String, RenderError>where
T: Serialize,
pub fn render<T>(&self, name: &str, data: &T) -> Result<String, RenderError>where
T: Serialize,
Render a registered template with some data into a string
name
is the template name you registred previouslyctx
is the data that implementsserde::Serialize
Returns rendered string or an struct with error information
sourcepub 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
sourcepub fn render_template<T>(
&self,
template_string: &str,
data: &T
) -> Result<String, TemplateRenderError>where
T: Serialize,
pub fn render_template<T>(
&self,
template_string: &str,
data: &T
) -> Result<String, TemplateRenderError>where
T: Serialize,
render a template string using current registry without register it
sourcepub fn render_template_to_write<T, W>(
&self,
template_string: &str,
data: &T,
writer: W
) -> Result<(), TemplateRenderError>where
T: Serialize,
W: Write,
pub fn render_template_to_write<T, W>(
&self,
template_string: &str,
data: &T,
writer: W
) -> Result<(), TemplateRenderError>where
T: Serialize,
W: Write,
render a template string using current registry without register it
sourcepub fn render_template_source_to_write<T, W>(
&self,
template_source: &mut dyn Read,
data: &T,
writer: W
) -> Result<(), TemplateRenderError>where
T: Serialize,
W: Write,
pub fn render_template_source_to_write<T, W>(
&self,
template_source: &mut dyn Read,
data: &T,
writer: W
) -> Result<(), TemplateRenderError>where
T: Serialize,
W: Write,
render a template source using current registry without register it