Struct minijinja::Environment
source · [−]pub struct Environment<'source> { /* private fields */ }
Expand description
An abstraction that holds the engine configuration.
This object holds the central configuration state for templates and their configuration. Instances of this type may be modified if no template were loaded so far. Modifications on environments after the first template was loaded will lead to surprising effects and undefined behavior. For instance overriding the auto escape callback will no longer have effects to an already loaded template.
The environment holds references to the source the templates were created from.
This makes it very inconvenient to pass around unless the templates are static
strings.
For situations where you want to load dynamic templates and share the
environment it’s recommended to turn on the source
feature and to use the
Source
type with the environment.
Implementations
sourceimpl<'source> Environment<'source>
impl<'source> Environment<'source>
sourcepub fn new() -> Environment<'source>
pub fn new() -> Environment<'source>
Creates a new environment with sensible defaults.
This environment does not yet contain any templates but it will have all the
default filters loaded. If you do not want any default configuration you
can use the alternative empty
method.
sourcepub fn empty() -> Environment<'source>
pub fn empty() -> Environment<'source>
Creates a completely empty environment.
This environment has no filters, no templates and no default logic for auto escaping configured.
sourcepub fn set_auto_escape_callback<F: Fn(&str) -> AutoEscape + 'static + Sync + Send>(
&mut self,
f: F
)
pub fn set_auto_escape_callback<F: Fn(&str) -> AutoEscape + 'static + Sync + Send>(
&mut self,
f: F
)
Sets a new function to select the default auto escaping.
This function is invoked when templates are loaded from the environment
to determine the default auto escaping behavior. The function is
invoked with the name of the template and can make an initial auto
escaping decision based on that. The default implementation is to
turn on escaping for templates ending with .html
, .htm
and .xml
.
sourcepub fn set_debug(&mut self, enabled: bool)
This is supported on crate feature debug
only.
pub fn set_debug(&mut self, enabled: bool)
debug
only.Enable or disable the debug mode.
When the debug mode is enabled the engine will dump out some of the execution state together with the source information of the executing template when an error is created. The cost of this is relatively high as the data including the template source is cloned.
However providing this information greatly improves the debug information
that the template error provides. When debug is enabled errors will
return a DebugInfo
object from
Error::debug_info
.
This requires the debug
feature.
sourcepub fn set_source(&mut self, source: Source)
This is supported on crate feature source
only.
pub fn set_source(&mut self, source: Source)
source
only.Sets the template source for the environment.
This helps when working with dynamically loaded templates. For more
information see Source
.
Already loaded templates in the environment are discarded and replaced with the templates from the source.
sourcepub fn get_source(&self) -> Option<&Source>
This is supported on crate feature source
only.
pub fn get_source(&self) -> Option<&Source>
source
only.Returns the currently set source.
sourcepub fn add_template(
&mut self,
name: &'source str,
source: &'source str
) -> Result<(), Error>
pub fn add_template(
&mut self,
name: &'source str,
source: &'source str
) -> Result<(), Error>
Loads a template from a string.
The name
parameter defines the name of the template which identifies
it. To look up a loaded template use the get_template
method.
sourcepub fn remove_template(&mut self, name: &str)
pub fn remove_template(&mut self, name: &str)
Removes a template by name.
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.
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
.
pub fn _compile_expression(
&self,
expr: &'source str
) -> Result<Expression<'_, 'source>, Error>
sourcepub fn add_filter<F, V, Rv, Args>(&mut self, name: &'source str, f: F) where
V: ArgType,
Rv: Into<Value>,
F: Filter<V, Rv, Args>,
Args: FunctionArgs,
pub fn add_filter<F, V, Rv, Args>(&mut self, name: &'source str, f: F) where
V: ArgType,
Rv: Into<Value>,
F: Filter<V, Rv, Args>,
Args: FunctionArgs,
Adds a new filter function.
For details about filters have a look at filters
.
sourcepub fn remove_filter(&mut self, name: &str)
pub fn remove_filter(&mut self, name: &str)
Removes a filter by name.
sourcepub fn add_test<F, V, Args>(&mut self, name: &'source str, f: F) where
V: ArgType,
F: Test<V, Args>,
Args: FunctionArgs,
pub fn add_test<F, V, Args>(&mut self, name: &'source str, f: F) where
V: ArgType,
F: Test<V, Args>,
Args: FunctionArgs,
Adds a new test function.
For details about tests have a look at tests
.
sourcepub fn remove_test(&mut self, name: &str)
pub fn remove_test(&mut self, name: &str)
Removes a test by name.
sourcepub fn add_function<F, Rv, Args>(&mut self, name: &'source str, f: F) where
Rv: Into<Value>,
F: Function<Rv, Args>,
Args: FunctionArgs,
pub fn add_function<F, Rv, Args>(&mut self, name: &'source str, f: F) where
Rv: Into<Value>,
F: Function<Rv, Args>,
Args: FunctionArgs,
Adds a new global function.
For details about functions have a look at functions
. Note that
functions and other global variables share the same namespace.
sourcepub fn add_global(&mut self, name: &'source str, value: Value)
pub fn add_global(&mut self, name: &'source str, value: Value)
Adds a global variable.
sourcepub fn remove_global(&mut self, name: &str)
pub fn remove_global(&mut self, name: &str)
Removes a global function or variable by name.
Trait Implementations
sourceimpl<'source> Clone for Environment<'source>
impl<'source> Clone for Environment<'source>
sourcefn clone(&self) -> Environment<'source>
fn clone(&self) -> Environment<'source>
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl<'source> Debug for Environment<'source>
impl<'source> Debug for Environment<'source>
Auto Trait Implementations
impl<'source> !RefUnwindSafe for Environment<'source>
impl<'source> Send for Environment<'source>
impl<'source> Sync for Environment<'source>
impl<'source> Unpin for Environment<'source>
impl<'source> !UnwindSafe for Environment<'source>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more