Expand description
Test functions and abstractions.
Test functions in MiniJinja are like (filters)crate::filters but a different syntax
is used to invoke them and they have to return boolean values. For instance the
expression {% if foo is odd %}
invokes the is_odd
test to check if the value
is indeed an odd number.
MiniJinja comes with some built-in test functions that are listed below. To
create a custom test write a function that takes at least a
&State
and value argument and returns a boolean
result, then register it with add_filter
.
Custom Tests
A custom test function is just a simple function which accepts inputs as parameters and then returns a bool wrapped in a result. For instance the following shows a test function which takes an input value and checks if it’s lowercase:
fn is_lowercase(_state: &State, value: String) -> Result<bool, Error> {
Ok(value.chars().all(|x| x.is_lowercase()))
}
env.add_test("lowercase", is_lowercase);
MiniJinja will perform the necessary conversions automatically via the
FunctionArgs
trait.
Traits
A utility trait that represents filters.
Functions
builtins
Checks if a value is defined.
builtins
Checks if the value is ending with a string.
builtins
Checks if a value is even.
builtins
Checks if this value is a mapping
builtins
Checks if this value is a number.
builtins
Checks if a value is odd.
builtins
Checks if this value is a sequence
builtins
Checks if the value is starting with a string.
builtins
Checks if this value is a string.
builtins
Checks if a value is undefined.