Home Explore Blog CI



tera

12th chunk of `docs/content/docs/_index.md`
171abb36e3ac73df363e19e7df2751afe0cda30a21e73a1b0000000100000fa2
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 (`/`).

Example: `{{ value | urlencode_strict }}`

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

#### abs
Returns the absolute value

Example: `{{ negative_number | abs }}`

If negative_number is -1, the output will be 1. If num_messages is -2.0 the output will be 2.

#### pluralize
Returns a plural suffix if the value is not equal to ±1, or a singular suffix otherwise. The plural suffix defaults to `s` and the
singular suffix defaults to the empty string (i.e. nothing).

Example: `You have {{ num_messages }} message{{ num_messages | pluralize }}`

If num_messages is 1, the output will be You have 1 message. If num_messages is 2 the output will be You have 2 messages. You can
also customize the singular and plural suffixes with the `singular` and `plural` arguments to the filter:

Example: `{{ num_categories }} categor{{ num_categories | pluralize(singular="y", plural="ies") }}`

#### round
Returns a number rounded following the method given. Default method is `common` which will round to the nearest integer.
`ceil` and `floor` are available as alternative methods.
Another optional argument, `precision`, is available to select the precision of the rounding. It defaults to `0`, which will
round to the nearest integer for the given method.

Example: `{{ num | round }} {{ num | round(method="ceil", precision=2) }}`

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

Returns a human-readable file size (i.e. '110 MB') from an integer.

Example: `{{ num | filesizeformat }}`

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

Parses a timestamp into a date(time) string. Defaults to `YYYY-MM-DD` format.
Time formatting syntax is inspired from strftime and a full reference is available
on [chrono docs](https://docs.rs/chrono/0.4/chrono/format/strftime/index.html).

Example: `{{ ts | date }} {{ ts | date(format="%Y-%m-%d %H:%M") }}`

If you are using ISO 8601 date strings or a UTC timestamp, you can optionally supply a timezone for the date to be rendered in.

Example:

```
{{ "2019-09-19T13:18:48.731Z" | date(timezone="America/New_York") }}

{{ "2019-09-19T13:18:48.731Z" | date(format="%Y-%m-%d %H:%M", timezone="Asia/Shanghai") }}

{{ 1648252203 | date(timezone="Europe/Berlin") }}
```

Locale can be specified (excepted when the input is a timestamp without timezone argument), default being POSIX. (only available if the `date-locale` feature is enabled)

Example: `{{ 1648252203 | date(format="%A %-d %B", timezone="Europe/Paris", locale="fr_FR") }}`

#### escape
Escapes a string's HTML. Specifically, it makes these replacements:

- `&` is converted to `&`
- `<` is converted to `&lt;`
- `>` is converted to `&gt;`
- `"` (double quote) is converted to `&quot;`
- `'` (single quote) is converted to `&#x27;`
- `/` is converted to `&#x2F;`

#### escape_xml
Escapes XML special characters. Specifically, it makes these replacements:

- `&` is converted to `&amp;`

Title: Tera Built-in Filters (Continued)
Summary
This section continues describing Tera's built-in filters. It explains `concat` which appends value(s) to an array. Then `urlencode` and `urlencode_strict` are introduced which encode strings for use in URLs, with `urlencode_strict` encoding more characters. After this `abs` returns the absolute value. Then `pluralize` adds a plural suffix if the value is not equal to ±1. Following this is `round` which rounds a number to a given precision. The `filesizeformat` returns a human-readable file size. Then `date` parses a timestamp into a formatted date string, allowing for timezone specification and locale. Finally, `escape` and `escape_xml` are described, which escape HTML and XML special characters respectively.