Module minijinja::filters

source ·
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 value, then registers it with add_filter.

Using Filters

Using filters in templates is possible in all places an expression is permitted. This means they are not just used for printing but also are useful for iteration or similar situations.

Motivating example:

<dl>
{% for key, value in config|items %}
  <dt>{{ key }}
  <dd><pre>{{ value|tojson }}</pre>
{% endfor %}
</dl>

Custom Filters

A custom filter is just a simple function which accepts its 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(value: String) -> String {
    value.to_lowercase().split_whitespace().collect::<Vec<_>>().join("-")
}

env.add_filter("slugify", slugify);

MiniJinja will perform the necessary conversions automatically. For more information see the Filter trait.

Accessing State

In some cases it can be necessary to access the execution State. Since a borrowed state implements ArgType it’s possible to add a parameter that holds the state. For instance the following filter appends the current template name to the string:

use minijinja::value::Value;
use minijinja::State;

fn append_template(state: &State, value: &Value) -> String {
    format!("{}-{}", value, state.name())
}

env.add_filter("appendTemplate", append_template);

Built-in Filters

When the builtins feature is enabled a range of built-in filters are automatically added to the environment. These are also all provided in this module. Note though that these functions are not to be called from Rust code as their exact interface (arguments and return types) might change from one MiniJinja version to another.

Traits

  • A utility trait that represents filters.

Functions

  • absbuiltins
    Returns the absolute value of a number.
  • attrbuiltins
    Looks up an attribute.
  • batchbuiltins
    Batch items.
  • boolbuiltins
    Converts the value into a boolean value.
  • capitalizebuiltins
    Convert the string with all its characters lowercased apart from the first char which is uppercased.
  • defaultbuiltins
    If the value is undefined it will return the passed default value, otherwise the value of the variable:
  • dictsortbuiltins
    Dict sorting functionality.
  • Escapes a string. By default to HTML.
  • firstbuiltins
    Returns the first item from a list.
  • indentbuiltins
    indents Value with spaces
  • itemsbuiltins
    Returns a list of pairs (items) from a mapping.
  • joinbuiltins
    Joins a sequence by a character
  • lastbuiltins
    Returns the last item from a list.
  • lengthbuiltins
    Returns the “length” of the value
  • listbuiltins
    Converts the input value into a list.
  • lowerbuiltins
    Converts a value to lowercase.
  • mapbuiltins
    Applies a filter to a sequence of objects or looks up an attribute.
  • maxbuiltins
    Returns the largest item from the list.
  • minbuiltins
    Returns the smallest item from the list.
  • pprintbuiltins
    Pretty print a variable.
  • rejectbuiltins
    Creates a new sequence of values that don’t pass a test.
  • rejectattrbuiltins
    Creates a new sequence of values of which an attribute does not pass a test.
  • replacebuiltins
    Does a string replace.
  • reversebuiltins
    Reverses a list or string
  • roundbuiltins
    Round the number to a given precision.
  • Marks a value as safe. This converts it into a string.
  • selectbuiltins
    Creates a new sequence of values that pass a test.
  • selectattrbuiltins
    Creates a new sequence of values of which an attribute passes a test.
  • slicebuiltins
    Slice an iterable and return a list of lists containing those items.
  • sortbuiltins
    Returns the sorted version of the given list.
  • titlebuiltins
    Converts a value to title case.
  • tojsonbuiltins and json
    Dumps a value to JSON.
  • trimbuiltins
    Trims a value
  • uniquebuiltins
    Returns a list of unique items from the given iterable.
  • upperbuiltins
    Converts a value to uppercase.
  • urlencodebuiltins and urlencode
    URL encodes a value.