pub struct Engines {
pub tera: Tera,
pub handlebars: Handlebars<'static>,
pub minijinja: Environment<'static>,
}Expand description
A structure exposing access to templating engines.
Calling methods on the exposed template engine types may require importing
types from the respective templating engine library. These types should be
imported from the reexported crate at the root of rocket_dyn_templates to
avoid version mismatches. For instance, when registering a Tera filter, the
tera::Value and tera::TeraResult types are required. Import them
from rocket_dyn_templates::tera. The example below illustrates this:
use rocket_dyn_templates::{Templating, Engines};
use rocket_dyn_templates::tera::{Value, Kwargs, State, TeraResult};
fn my_filter(value: &Value, _: Kwargs, _: &State) -> TeraResult<Value> {
...
}
fn main() {
rocket::build()
// ...
.attach(Templating::init(|engines: &mut Engines| {
engines.tera.register_filter("my_filter", my_filter);
}))
// ...
}Fields§
§tera: TeraA Tera templating engine.
This field is only available when the tera feature is enabled. When
calling methods on the Tera instance, ensure you use types imported
from rocket_dyn_templates::tera to avoid version mismatches.
§Escaping
HTML escaping is enabled for template names ending in .html, .htm,
.xml, .xhtml, or .svg, regardless of ASCII case, optionally
followed by .tera. Included templates use the escaping policy of the
template that includes them. To change these defaults, call
Tera::autoescape_on() or Tera::set_escape_fn() from a
Templating::init() callback.
Only the template’s name is considered. A template added from a file
under a different name, as with add_template_file(path, Some(name)),
is escaped only if name ends in one of the extensions above,
regardless of the file’s extension.
§Template Loading
Tera checks a template as soon as it’s added: every filter, test,
function, component, and template it uses must already exist.
Similarly, delimiters cannot be changed once a template has been added.
As a result, the order of Templating callbacks matters for Tera.
See customization for which to use.
Templates added together can use each other: Rocket adds all templates
in template_dir at once, as does Tera::add_raw_templates(). A
template in template_dir cannot, however, use a template added in an
on_load callback. If templates must use each other in both
directions, place them all in template_dir.
handlebars: Handlebars<'static>The Handlebars templating engine.
This field is only available when the handlebars feature is enabled.
When calling methods on the Handlebars instance, ensure you use types
imported from rocket_dyn_templates::handlebars to avoid version
mismatches.
Handlebars uses HTML escaping regardless of the template name. To
change this default, call Handlebars::register_escape_fn() from a
Templating::init() callback.
Handlebars parses a template when it’s added but looks up helpers and
partials only when rendering. As such, Handlebars templates can use
helpers and templates registered in either
Templating callback.
minijinja: Environment<'static>The minijinja templating engine.
This field is only available when the minijinja feature is enabled.
When calling methods on the Environment instance, ensure you use
types imported from rocket_dyn_templates::minijinja to avoid version
mismatches.
§Escaping
HTML escaping is enabled for template names ending in .html, .htm,
.xml, .xhtml, or .svg, regardless of ASCII case. The lowercase
extensions .json, .json5, .js, .yaml, and .yml enable JSON
escaping. A final .j2, .jinja, or .jinja2 extension is ignored.
Included templates use their own escaping policy. As such, HTML
fragments should use an HTML extension: including a text template in an
HTML template can render values without escaping them. To change these
defaults, call Environment::set_auto_escape_callback() from a
Templating::init() callback.
§Template Loading
MiniJinja parses templates in template_dir when they are first
rendered, not when Rocket loads them, and looks up filters, tests,
functions, and other templates only when rendering. As such, MiniJinja
templates can use anything registered in either
Templating callback. A template added via
Environment::add_template(), however, is parsed immediately using
the current syntax and escaping settings, so configure these first.
Because templates in template_dir are parsed when rendered, a syntax
error is only reported then. Rocket cannot detect such an error when it
reloads templates, so the previous templates are not restored.
Rocket loads templates in template_dir via the environment’s loader
and reloads them in debug mode, so MiniJinja’s own automatic reloading
is disabled. Replacing the loader via Environment::set_loader()
makes templates in template_dir unavailable.
Auto Trait Implementations§
impl !Freeze for Engines
impl !RefUnwindSafe for Engines
impl !UnwindSafe for Engines
impl Forget for Engines
impl Move for Engines
impl Send for Engines
impl Sync for Engines
impl Unpin for Engines
impl UnsafeUnpin for Engines
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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>
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>
Source§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
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>
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>
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>
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>
Source§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
Source§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
Source§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
Source§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
Source§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
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 rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
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>
renamed to 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);