Home Explore Blog CI



tera

13th chunk of `docs/content/docs/_index.md`
e7ab476fd96fcb652fa6c3b4e766a9e5cddc76535c6a76f70000000100000eb8
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;`
- `<` is converted to `&lt;`
- `>` is converted to `&gt;`
- `"` (double quote) is converted to `&quot;`
- `'` (single quote) is converted to `&apos;`

#### safe
Marks a variable as safe: HTML will not be escaped anymore.
`safe` only works if it is the last filter of the expression:

- `{{ content | replace(from="Robert", to="Bob") | safe }}` will not be escaped
- `{{ content | safe | replace(from="Robert", to="Bob") }}` will be escaped

#### get
Accesses a value from an object when the key is not a Tera identifier.
Example: `{{ sections | get(key="posts/content") }}`

The `get` filter also has a `default` parameter which can be used to provide a return value when the `key` parameter is missing from the set being filtered.
Example: `{{ sections | get(key="posts/content", default="default") }}`

#### split
Splits a string into an array of strings, separated by a pattern given.
Example: `{{ path | split(pat="/") }}`

#### int
Converts a value into an integer.  The `default` argument can be used to specify the value to return on error, and the `base` argument can be used to specify how to interpret the number.  Bases of 2, 8, and 16 understand the prefix 0b, 0o, 0x, respectively.

#### float
Converts a value into a float.  The `default` argument can be used to specify the value to return on error.

#### json_encode
Transforms any value into a JSON representation. This filter is better used together with `safe` or when automatic escape is disabled.

Example: `{{ value | json_encode() | safe }}`

It accepts a parameter `pretty` (boolean) to print a formatted JSON instead of a one-liner.

Example: `{{ value | json_encode(pretty=true) | safe }}`

#### as_str
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

Title: Tera Built-in Filters (Continued)
Summary
This section continues describing Tera's built-in filters. It covers `date` (timezones/locales), `escape`/`escape_xml` (HTML/XML escaping), `safe` (disables escaping), `get` (access object values), `split` (string splitting), `int` (integer conversion), `float` (float conversion), `json_encode` (JSON representation), `as_str` (string conversion), and `default` (provides default values).