Home Explore Blog CI



tera

15th chunk of `docs/content/docs/_index.md`
a13120ce04a5e7081b997c63f7162dc1d211d6818a5b92450000000100000cdc
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?
- maps: is the arg given a key of the map?

Example:
```jinja2
{% if username is containing("xXx") %}
    Bad
{% endif %}
```

#### matching
Returns true if the given variable is a string and matches the regex in the argument.

Example:
```jinja2
{% if name is matching("^[Qq]ueen") %}
    Her Royal Highness, {{ name }}
{% elif name is matching("^[Kk]ing") %}
    His Royal Highness, {{ name }}
{% else %}
    {{ name }}
{% endif %}
```

A comprehensive syntax description can be found in the [regex crate documentation](https://docs.rs/regex/).

### Built-in functions
Tera comes with some built-in global functions.

#### range

Returns an array of integers created using the arguments given.
There are 3 arguments, all integers:

- `end`: stop before `end`, mandatory
- `start`: where to start from, defaults to `0`
- `step_by`: with what number do we increment, defaults to `1`


#### now
Only available if the `builtins` feature is enabled.

Returns the local datetime as string or the timestamp as integer if requested.

There are 2 arguments, both booleans:

- `timestamp`: whether to return the timestamp instead of the datetime
- `utc`: whether to return the UTC datetime instead of the local one

Formatting is not built-in the global function but you can use the `date` filter like so `now() | date(format="%Y")` if you
wanted to get the current year.

#### throw
The template rendering will error with the given message when encountered.

There is only one string argument:

- `message`: the message to display as the error

#### get_random
Only available if the `builtins` feature is enabled.

Returns a random integer in the given range. There are 2 arguments, both integers:

- `start`: defaults to 0 if not present
- `end`: required

`start` is inclusive (i.e. can be returned) and `end` is exclusive.

#### get_env
Returns the environment variable value for the name given. It will error if the environment variable is not found
but the call can also take a default value instead.

- `name`: the name of the environment variable to look for, required
- `default`: a default value in case the environment variable is not found

If the environment variable is found, it will always be a string while your default could be of any type.

Title: Tera Built-in Tests (continued) and Functions
Summary
This section continues the description of Tera's built-in tests, covering `matching` (checks if a string matches a regex pattern). It then introduces Tera's built-in global functions: `range` (creates an array of integers), `now` (returns current datetime/timestamp), `throw` (causes template rendering error with custom message), `get_random` (returns a random integer within a range), and `get_env` (retrieves environment variable value or a default value).