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

defaultbuiltin_filters

Checks if a string starts with another string.

dictsortbuiltin_filters

Dict sorting functionality.

HTML escapes a string.

joinbuiltin_filters

Joins a sequence by a character

lengthbuiltin_filters

Returns the “length” of the value

lowerbuiltin_filters

Converts a value to lowercase.

replacebuiltin_filters

Does a string replace.

reversebuiltin_filters

Reverses a list or string

Marks a value as safe. This converts it into a string.

tojsonbuiltin_filters and json

Dumps a value to JSON.

trimbuiltin_filters

Trims a value

upperbuiltin_filters

Converts a value to uppercase.

urlencodebuiltin_filters and urlencode

URL encodes a value.