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 a
&State
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(_state: &State, 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
builtin_filters
Checks if a string starts with another string.
builtin_filters
Dict sorting functionality.
HTML escapes a string.
builtin_filters
Joins a sequence by a character
builtin_filters
Returns the “length” of the value
builtin_filters
Converts a value to lowercase.
builtin_filters
Does a string replace.
builtin_filters
Reverses a list or string
Marks a value as safe. This converts it into a string.
builtin_filters
and json
Dumps a value to JSON.
builtin_filters
Trims a value
builtin_filters
Converts a value to uppercase.
builtin_filters
and urlencode
URL encodes a value.