Expand description
Global functions and abstractions.
This module provides the abstractions for functions that can registered as
global functions to the environment via
add_function
.
Using Functions
Functions can be called in any place where an expression is valid. They
are useful to retrieve data. Some functions are special and provided
by the engine (like super
) within certain context, others are global.
The following is a motivating example:
<pre>{{ debug() }}</pre>
Custom Functions
A custom global function is just a simple rust function which accepts the
&State
as first argument, optionally some additional
arguments and then returns a result. Global functions are typically used to
perform a data loading operation. For instance these functions can be used
to expose data to the template that hasn’t been provided by the individual
render invocation.
fn include_file(_state: &State, name: String) -> Result<String, Error> {
std::fs::read_to_string(&name)
.map_err(|e| Error::new(
ErrorKind::ImpossibleOperation,
"cannot load file"
).with_source(e))
}
env.add_function("include_file", include_file);
Traits
A utility trait that represents global functions.