Expand description
Test functions and abstractions.
Test functions in MiniJinja are like filters
but a
different syntax is used to invoke them and they have to return boolean
values. For instance the expression {% if foo is defined %}
invokes the
is_defined
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 value argument
that returns a boolean result, then register it with
add_filter
.
§Using Tests
Tests are useful to “test” a value in a specific way. For instance if
you want to assign different classes to alternating rows one way is
using the odd
test:
{% if seq is defined %}
<ul>
{% for item in seq %}
<li class="{{ 'even' if loop.index is even else 'odd' }}">{{ item }}</li>
{% endfor %}
</ul>
{% endif %}
§Custom Tests
A custom test function is just a simple function which accepts its inputs as parameters and then returns a bool. For instance the following shows a test function which takes an input value and checks if it’s lowercase:
fn is_lowercase(value: String) -> bool {
value.chars().all(|x| x.is_lowercase())
}
env.add_test("lowercase", is_lowercase);
MiniJinja will perform the necessary conversions automatically. For more
information see the Function
trait. If a
test returns a value that is not a bool, it will be evaluated for truthiness
with Value::is_true
.
§Built-in Tests
When the builtins
feature is enabled a range of built-in tests 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§
- Test
- A utility trait that represents global functions.
- Test
Result - A utility trait that represents the return value of functions, filters and tests.
Functions§
- is_
boolean - Return true if the object is a boolean value.
- is_
defined - Checks if a value is defined.
- is_
divisibleby - Return true if the value is divisible by another one.
- is_
endingwith - Checks if the value is ending with a string.
- is_eq
- Test version of
==
. - is_even
- Checks if a value is even.
- is_
false - Checks if a value is
false
. - is_
filter - Checks if a filter with a given name is available.
- is_
float - Checks if this value is a float
- is_ge
- Test version of
>=
. - is_gt
- Test version of
>
. - is_in
- Test version of
in
. - is_
integer - Checks if this value is an integer.
- is_
iterable - Checks if this value can be iterated over.
- is_le
- Test version of
<=
. - is_
lower - Checks if a string is all lowercase.
- is_lt
- Test version of
<
. - is_
mapping - Checks if this value is a mapping
- is_ne
- Test version of
!=
. - is_none
- Checks if a value is none.
- is_
number - Checks if this value is a number.
- is_odd
- Checks if a value is odd.
- is_safe
- Checks if a value is safe.
- is_
sameas - Checks if two values are identical.
- is_
sequence - Checks if this value is a sequence
- is_
startingwith - Checks if the value is starting with a string.
- is_
string - Checks if this value is a string.
- is_test
- Checks if a test with a given name is available.
- is_true
- Checks if a value is
true
. - is_
undefined - Checks if a value is undefined.
- is_
upper - Checks if a string is all uppercase.