Home Explore Blog CI



tera

14th chunk of `docs/content/docs/_index.md`
ee7da8cd43b9cf72248a967e4c57968f9c937aad32ac4916000000010000093e
Returns a string representation of the given value.

Example: `{{ value | as_str }}`

#### default
Returns the default value given only if the variable evaluated is not present in the context
and is therefore meant to be at the beginning of a filter chain if there are several filters.

Example: `{{ value | default(value=1) }}`

This is in most cases a shortcut for:

```jinja2
{% if value %}{{ value }}{% else %}1{% endif %}
```

However, only the existence of the value in the context is checked. With a value that `if` would
evaluate to false (such as an empty string, or the number 0), the `default` filter will not attempt
replace it with the alternate value provided. For example, the following will produce
"I would like to read more !":

```jinja2
I would like to read more {{ "" | default (value="Louise Michel") }}!
```

If you intend to use the default filter to deal with optional values, you should make sure those values
aren't set! Otherwise, use a full `if` block. This is especially relevant for dealing with optional arguments
passed to a macro.

### Built-in tests

Here are the currently built-in tests:

#### defined
Returns true if the given variable is defined.

#### undefined
Returns true if the given variable is undefined.

#### odd
Returns true if the given variable is an odd number.

#### even
Returns true if the given variable is an even number.

#### string
Returns true if the given variable is a string.

#### number
Returns true if the given variable is a number.

#### divisibleby
Returns true if the given expression is divisible by the arg given.

Example:
```jinja2
{% if rating is divisibleby(2) %}
    Divisible
{% endif %}
```

#### iterable
Returns true if the given variable can be iterated over in Tera (i.e. is an array/tuple or an object).

#### object
Returns true if the given variable is an object (i.e. can be iterated over key, value).

#### starting\_with
Returns true if the given variable is a string and starts with the arg given.

Example:
```jinja2
{% if path is starting_with("x/") %}
    In section x
{% endif %}
```

#### ending\_with
Returns true if the given variable is a string and ends with the arg given.

#### containing
Returns true if the given variable contains the arg given.

The test works on:

- strings: is the arg a substring?
- arrays: is the arg given one of the members of the array?

Title: Tera Built-in Filters (default) and Tests
Summary
This section describes the `default` filter, which provides a default value if a variable is not present in the context. It also covers Tera's built-in tests: `defined`/`undefined` (checks if a variable is defined), `odd`/`even` (checks if a number is odd/even), `string`/`number` (checks the variable type), `divisibleby` (checks divisibility), `iterable`/`object` (checks if a variable is iterable/an object), and `starting_with`/`ending_with`/`containing` (checks string prefixes/suffixes/substrings or array membership).