Struct mailpot_web::minijinja_utils::TEMPLATES
source · pub struct TEMPLATES {
__private_field: (),
}
Fields§
§__private_field: ()
Methods from Deref<Target = Environment<'static>>§
sourcepub fn get_template(&self, name: &str) -> Result<Template<'_>, Error>
pub fn get_template(&self, name: &str) -> Result<Template<'_>, Error>
Fetches a template by name.
This requires that the template has been loaded with
add_template
beforehand. If the template was
not loaded an error of kind TemplateNotFound
is returned.
let mut env = Environment::new();
env.add_template("hello.txt", "Hello {{ name }}!").unwrap();
let tmpl = env.get_template("hello.txt").unwrap();
println!("{}", tmpl.render(context!{ name => "World" }).unwrap());
sourcepub fn render_str<S>(&self, source: &str, ctx: S) -> Result<String, Error>where
S: Serialize,
pub fn render_str<S>(&self, source: &str, ctx: S) -> Result<String, Error>where S: Serialize,
Parses and renders a template from a string in one go.
In some cases you really only need a template to be rendered once from
a string and returned. The internal name of the template is <string>
.
let env = Environment::new();
let rv = env.render_str("Hello {{ name }}", context! { name => "World" });
println!("{}", rv.unwrap());
Note on values: The Value
type implements Serialize
and can be
efficiently passed to render. It does not undergo actual serialization.
sourcepub fn render_named_str<S>(
&self,
name: &str,
source: &str,
ctx: S
) -> Result<String, Error>where
S: Serialize,
pub fn render_named_str<S>( &self, name: &str, source: &str, ctx: S ) -> Result<String, Error>where S: Serialize,
Parses and renders a template from a string in one go with name.
Like render_str
, but provide a name for the
template to be used instead of the default <string>
.
let env = Environment::new();
let rv = env.render_named_str(
"template_name",
"Hello {{ name }}",
context! { name => "World" }
);
println!("{}", rv.unwrap());
Note on values: The Value
type implements Serialize
and can be
efficiently passed to render. It does not undergo actual serialization.
sourcepub fn undefined_behavior(&self) -> UndefinedBehavior
pub fn undefined_behavior(&self) -> UndefinedBehavior
Returns the current undefined behavior.
This is particularly useful if a filter function or similar wants to change its behavior with regards to undefined values.
sourcepub fn compile_expression(
&self,
expr: &'source str
) -> Result<Expression<'_, 'source>, Error>
pub fn compile_expression( &self, expr: &'source str ) -> Result<Expression<'_, 'source>, Error>
Compiles an expression.
This lets one compile an expression in the template language and
receive the output. This lets one use the expressions of the language
be used as a minimal scripting language. For more information and an
example see Expression
.