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.


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.

Creates a completely empty environment.

This environment has no filters, no templates and no default logic for auto escaping configured.

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.

This is supported on crate feature 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.

This is supported on crate feature 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.

This is supported on crate feature source only.

Returns the currently set source.

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.

Removes a template by name.

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.

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.

Adds a new filter function.

For details about filters have a look at filters.

Removes a filter by name.

Adds a new test function.

For details about tests have a look at tests.

Removes a test by name.

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.

Adds a global variable.

Removes a global function or variable by name.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.