Struct rocket_dyn_templates::handlebars::Handlebars
source · pub struct Handlebars<'reg> { /* private fields */ }
Expand description
The single entry point of your Handlebars templates
It maintains compiled templates and registered helpers.
Implementations§
source§impl<'reg> Registry<'reg>
impl<'reg> Registry<'reg>
pub fn new() -> Registry<'reg>
sourcepub fn set_strict_mode(&mut self, enabled: bool)
pub fn set_strict_mode(&mut self, enabled: bool)
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.
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 dev_mode(&self) -> bool
pub fn dev_mode(&self) -> bool
Return dev mode state, default is false
With dev mode turned on, handlebars enables a set of development friendly features, that may affect its performance.
sourcepub fn set_dev_mode(&mut self, enabled: bool)
pub fn set_dev_mode(&mut self, enabled: bool)
Enable or disable dev mode
With dev mode turned on, handlebars enables a set of development friendly 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.
sourcepub fn set_prevent_indent(&mut self, enable: bool)
pub fn set_prevent_indent(&mut self, enable: bool)
Enable or disable indent for partial include tag {{>}}
By default handlebars keeps indent whitespaces for partial
include tag, to change this behaviour, set this toggle to true
.
sourcepub fn prevent_indent(&self) -> bool
pub fn prevent_indent(&self) -> bool
Return state for prevent_indent
option, default to false
.
sourcepub fn register_template(&mut self, name: &str, tpl: Template)
pub fn register_template(&mut self, name: &str, tpl: Template)
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.
sourcepub fn register_template_string<S>(
&mut self,
name: &str,
tpl_str: S,
) -> Result<(), TemplateError>
pub fn register_template_string<S>( &mut self, name: &str, tpl_str: S, ) -> Result<(), TemplateError>
Register a template string
Returns TemplateError
if there is syntax error on parsing the template.
sourcepub fn register_partial<S>(
&mut self,
name: &str,
partial_str: S,
) -> Result<(), TemplateError>
pub fn register_partial<S>( &mut self, name: &str, partial_str: S, ) -> Result<(), TemplateError>
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.
sourcepub fn register_template_file<P>(
&mut self,
name: &str,
tpl_path: P,
) -> Result<(), TemplateError>
pub fn register_template_file<P>( &mut self, name: &str, tpl_path: P, ) -> Result<(), TemplateError>
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.
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 + Sync + Send + 'reg>,
)
pub fn register_helper( &mut self, name: &str, def: Box<dyn HelperDef + Sync + Send + 'reg>, )
Register a helper
sourcepub fn register_decorator(
&mut self,
name: &str,
def: Box<dyn DecoratorDef + Sync + Send + 'reg>,
)
pub fn register_decorator( &mut self, name: &str, def: Box<dyn DecoratorDef + Sync + Send + 'reg>, )
Register a decorator
sourcepub fn register_escape_fn<F>(&mut self, escape_fn: F)
pub fn register_escape_fn<F>(&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_templates(&self) -> &HashMap<String, Template>
pub fn get_templates(&self) -> &HashMap<String, 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.
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 registered previouslydata
is the data that implementsserde::Serialize
Returns rendered string or a struct with error information
sourcepub fn render_with_context(
&self,
name: &str,
ctx: &Context,
) -> Result<String, RenderError>
pub fn render_with_context( &self, name: &str, ctx: &Context, ) -> Result<String, RenderError>
Render a registered template with reused context
sourcepub fn render_to_write<T, W>(
&self,
name: &str,
data: &T,
writer: W,
) -> Result<(), RenderError>
pub fn render_to_write<T, W>( &self, name: &str, data: &T, writer: W, ) -> Result<(), RenderError>
Render a registered template and write data to the std::io::Write
sourcepub fn render_with_context_to_write<W>(
&self,
name: &str,
ctx: &Context,
writer: W,
) -> Result<(), RenderError>where
W: Write,
pub fn render_with_context_to_write<W>(
&self,
name: &str,
ctx: &Context,
writer: W,
) -> Result<(), RenderError>where
W: Write,
Render a registered template using reusable Context
, and write data to
the std::io::Write
sourcepub 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
sourcepub 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 reusable context data
sourcepub fn render_template_with_context_to_write<W>(
&self,
template_string: &str,
ctx: &Context,
writer: W,
) -> Result<(), RenderError>where
W: Write,
pub fn render_template_with_context_to_write<W>(
&self,
template_string: &str,
ctx: &Context,
writer: W,
) -> Result<(), RenderError>where
W: Write,
Render a template string using resuable context, and write data into
std::io::Write
sourcepub fn render_template_to_write<T, W>(
&self,
template_string: &str,
data: &T,
writer: W,
) -> Result<(), RenderError>
pub fn render_template_to_write<T, W>( &self, template_string: &str, data: &T, writer: W, ) -> Result<(), RenderError>
Render a template string using current registry without registering it
Trait Implementations§
Auto Trait Implementations§
impl<'reg> Freeze for Registry<'reg>
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§
source§impl<T> AsAny for Twhere
T: Any,
impl<T> AsAny for Twhere
T: Any,
fn as_any_ref(&self) -> &(dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
source§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the foreground set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red()
and
green()
, which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg()
:
use yansi::{Paint, Color};
painted.fg(Color::White);
Set foreground color to white using white()
.
use yansi::Paint;
painted.white();
source§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.bright_black());
source§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
source§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.bright_green());
source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.bright_yellow());
source§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
source§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.bright_magenta());
source§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
source§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
Returns self
with the
fg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.bright_white());
source§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self
with the background set to
value
.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red()
and
on_green()
, which have the same functionality but
are pithier.
§Example
Set background color to red using fg()
:
use yansi::{Paint, Color};
painted.bg(Color::Red);
Set background color to red using on_red()
.
use yansi::Paint;
painted.on_red();
source§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
source§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
source§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlack
.
§Example
println!("{}", value.on_bright_black());
source§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
source§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightGreen
.
§Example
println!("{}", value.on_bright_green());
source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightYellow
.
§Example
println!("{}", value.on_bright_yellow());
source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightBlue
.
§Example
println!("{}", value.on_bright_blue());
source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightMagenta
.
§Example
println!("{}", value.on_bright_magenta());
source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightCyan
.
§Example
println!("{}", value.on_bright_cyan());
source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
Returns self
with the
bg()
set to
Color::BrightWhite
.
§Example
println!("{}", value.on_bright_white());
source§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling Attribute
value
.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold()
and
underline()
, which have the same functionality
but are pithier.
§Example
Make text bold using attr()
:
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);
Make text bold using using bold()
.
use yansi::Paint;
painted.bold();
source§fn underline(&self) -> Painted<&T>
fn underline(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::Underline
.
§Example
println!("{}", value.underline());
source§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
Returns self
with the
attr()
set to
Attribute::RapidBlink
.
§Example
println!("{}", value.rapid_blink());
source§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi
Quirk
value
.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask()
and
wrap()
, which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk()
:
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);
Enable wrapping using wrap()
.
use yansi::Paint;
painted.wrap();
source§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting()
due to conflicts with Vec::clear()
.
The clear()
method will be removed in a future release.source§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the Condition
value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted
only when both stdout
and stderr
are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);