Home Explore Blog CI



tera

10th chunk of `docs/content/docs/_index.md`
e1b7b4023b3f50cd0fec3c2f92799303942b73a3c94afea70000000100000fad
Indents a string by injecting a prefix at the start of each line.  The `prefix` argument (default 4 spaces) specifies the prefix to insert per line.  If the `first` argument (default false) is set true spaces are inserted for the first line.  If the `blank` argument (default false) is set true spaces are inserted for blank/whitespace lines.

#### striptags
Tries to remove HTML tags from input. Does not guarantee well formed output if input is not valid HTML.

Example: `{{ value | striptags }}`

If value is "<b>Joel</b>", the output will be "Joel".

Note that if the template you are using it in is automatically escaped, you will need to call the `safe` filter
after `striptags`.

#### first
Returns the first element of an array.
If the array is empty, returns empty string.

#### last
Returns the last element of an array.
If the array is empty, returns empty string.

#### nth
Returns the nth element of an array.§
If the array is empty, returns empty string.
It takes a required `n` argument, corresponding to the 0-based index you want to get.

Example: `{{ value | nth(n=2) }}`

#### join
Joins an array with a string.

Example: `{{ value | join(sep=" // ") }}`

If value is the array `['a', 'b', 'c']`, the output will be the string "a // b // c".

#### length
Returns the length of an array, an object, or a string.

#### reverse
Returns a reversed string or array.

#### sort
Sorts an array into ascending order.

The values in the array must be a sortable type:
- numbers are sorted by their numerical value.
- strings are sorted in alphabetical order.
- arrays are sorted by their length.
- bools are sorted as if false=0 and true=1

If you need to sort a list of structs or tuples, use the `attribute`
argument to specify which field to sort by.

Example:

Given `people` is an array of Person

```rust
struct Name(String, String);

struct Person {
    name: Name,
    age: u32,
}
```

The `attribute` argument can be used to sort by last name:

```jinja2
{{ people | sort(attribute="name.1") }}
```

or by age:

```jinja2
{{ people | sort(attribute="age") }}
```

#### unique
Removes duplicate items from an array.  The `attribute` argument can be used to select items based on the values of an inner attribute.  For strings, the `case_sensitive` argument (default is false) can be used to control the comparison.

Example:

Given `people` is an array of Person

```rust
struct Name(String, String);

struct Person {
    name: Name,
    age: u32,
}
```

The `attribute` argument can be used to select one Person for each age:

```jinja2
{{ people | unique(attribute="age") }}
```

or by last name:

```jinja2
{{ people | unique(attribute="name.1", case_sensitive="true") }}
```

#### slice
Slices an array by the given `start` and `end` parameter. Both parameters are
optional and omitting them will return the same array.
Use the `start` argument to define where to start (inclusive, default to `0`)
and `end` argument to define where to stop (exclusive, default to the length of the array).
`start` and `end` are 0-indexed.

```jinja2
{% for i in my_arr | slice(end=5) %}
{% for i in my_arr | slice(start=1) %}
{% for i in my_arr | slice(start=1, end=5) %}
```

You can also use negative index values to refer the array from the last element. -1 refers to the
last index, -2 refers to the second last index and so on.

For example, let's look at the following template:

```jinja2
{% for i in my_arr | slice(end=-2) %}
```

will produce the follow output for `my_array = [1, 2, 3, 4, 5]`: `[1, 2, 3]`


#### group_by
Groups an array using the required `attribute` argument. The filter takes an array and returns
a map where the keys are the values of the `attribute` stringified and the values are all elements of
the initial array having that `attribute`. Values with missing `attribute` or where `attribute` is null
will be discarded.

Example:

Given `posts` is an array of Post

```rust
struct Author {
    name: String,
};

struct Post {
    content: String,
    year: u32,

Title: Tera Built-in Filters (Continued)
Summary
This section describes more of Tera's built-in filters: indent, striptags, first, last, nth, join, length, reverse, sort, unique, slice, and group_by, complete with examples and explanations of their parameters.