Home Explore Blog Models CI



tera

11th chunk of `docs/content/docs/_index.md`
3035b4ff4076d6b0a12b443142053b78e3fc7285407ec591000000010000100d
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,
    author: Author,
}
```

The `attribute` argument can be used to group posts by year:

```jinja2
{{ posts | group_by(attribute="year") }}
```

or by author name:

```jinja2
{% for name, author_posts in posts | group_by(attribute="author.name") %}
    {{ name }}
    {% for post in author_posts %}
        {{ post.year }}: {{ post.content }}
    {% endfor %}
{% endfor %}
```

Manipulating the hashmap produced by `group_by` in an arbitrary order requires additional steps to extract the keys into a separate array.

Example:

```jinja2
{% set map = section.pages | group_by(attribute="year") %}
{% set_global years = [] %}
{% for year, ignored in map %}
    {% set_global years = years | concat(with=year) %}
{% endfor %}
{% for year in years | reverse %}
    {% set posts = map[year] %}
{% endfor %}
```

#### filter

Filters the array values, returning only the values where the `attribute` is equal to the `value`.
Values with missing `attribute` or where `attribute` is null will be discarded.

`attribute` is mandatory.


Example:

Given `posts` is an array of Post

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

struct Post {
    content: String,
    year: u32,
    author: Author,
    draft: bool,
}
```

The `attribute` argument can be used to filter posts by draft value:

```jinja2
{{ posts | filter(attribute="draft", value=true) }}
```

or by author name:

```jinja2
{{ posts | filter(attribute="author.name", value="Vincent") }}
```

If `value` is not passed, it will drop any elements where the attribute is `null`.

#### map

Retrieves an attribute from each object in an array.  The `attribute` argument is mandatory and specifies what to extract.

Example:

Given `people` is an array of Person

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

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

The `attribute` argument is used to retrieve their ages.

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

#### concat
Appends values to an array.

```jinja2
{{ posts | concat(with=drafts) }}
```

The filter takes an array and returns a new array with the value(s) from the `with` parameter
added. If the `with` parameter is an array, all of its values will be appended one by one to the new array and
not as an array.

This filter can also be used to append a single value to an array if the value passed to `with` is not an array:

```jinja2
{% set pages_id = pages_id | concat(with=id) %}
```

The `with` attribute is mandatory.

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

Percent-encodes all the characters in a string which are not included in
unreserved chars (according to [RFC3986](https://tools.ietf.org/html/rfc3986)) with the exception of forward
slash (`/`).

Example: `{{ value | urlencode }}`

If value is `/foo?a=b&c=d`, the output will be `/foo%3Fa%3Db%26c%3Dd`. `/` is not escaped.

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

Similar to `urlencode` filter but encodes all non-alphanumeric characters in a string including forward slashes (`/`).

Title: Tera Built-in Filters (Continued)
Summary
This section continues the description of Tera's built-in filters, delving into more advanced operations and providing detailed examples. It covers the `group_by` filter, which groups array elements based on a specified attribute, creating a map for easy access. The `filter` filter allows for selecting array elements based on an attribute's value. The `map` filter retrieves a specific attribute from each object in an array, creating a new array of those attributes. The `concat` filter appends values to an existing array. Lastly, `urlencode` and `urlencode_strict` are introduced, which are responsible for encoding strings for use in URLs, with `urlencode_strict` encoding more characters than `urlencode`. Each filter's usage, parameters, and expected behavior are clearly explained with code snippets to illustrate their application, making it easier for developers to understand and utilize them effectively in their Tera templates.