Module minijinja::filters [−][src]
Expand description
Filter functions and abstractions.
MiniJinja inherits from Jinja2 the concept of filter functions. These are functions
which are applied to values to modify them. For example the expression {{ 42|filter(23) }}
invokes the filter filter
with the arguments 42
and 23
.
MiniJinja comes with some built-in filters that are listed below. To create a
custom filter write a function that takes at least an
&Environment
and value argument, then register it
with add_filter
.
Custom Filters
A custom filter is just a simple function which accepts inputs as parameters and then returns a new value. For instance the following shows a filter which takes an input value and replaces whitespace with dashes and converts it to lowercase:
fn slugify(env: &Environment, value: String) -> Result<String, Error> {
Ok(value.to_lowercase().split_whitespace().collect::<Vec<_>>().join("-"))
}
env.add_filter("slugify", slugify);
MiniJinja will perform the necessary conversions automatically via the
FunctionArgs
and Into
traits.
Traits
A utility trait that represents filters.
Functions
Dict sorting functionality.
HTML escapes a string.
Joins a sequence by a character
Returns the “length” of the value
Converts a value to lowercase.
Does a string replace.
Reverses a list or string
Marks a value as safe. This converts it into a string.
Trims a value
Converts a value to uppercase.