Home Explore Blog CI



tera

3rd chunk of `docs/content/docs/_index.md`
faab7669cf5b356a11cc1e1380c335ec0b6ea54b042eadd10000000100000fce
  Hello {{ name }}
{% endraw %}
```
would be rendered as `Hello {{ name }}`.

### Whitespace control

Tera comes with easy to use whitespace control: use `{%-` if you want to remove all whitespace
before a statement and `-%}` if you want to remove all whitespace after. This behavior also 
works with expressions, using `{{-` and `-}}`, and with comments, using `{#-` and `-#}`.

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

```jinja2
{% set my_var = 2 %}
{{ my_var }}
```

will have the following output:

```html

2
```

If we want to get rid of the empty line, we can write the following:

```jinja2
{% set my_var = 2 -%}
{{ my_var }}
```

### Comments
To comment out part of the template, wrap it in `{# #}`. Anything in between those tags
will not be rendered.

```jinja2
{# A comment #}
```

## Data structures

### Literals

Tera has a few literals that can be used:

- booleans: `true` (or `True`) and `false` (or `False`)
- integers
- floats
- strings: text delimited by `""`, `''` or ` `` `
- arrays: a comma-separated list of literals and/or idents surrounded by `[` and `]` (trailing comma allowed)

### Variables

Variables are defined by the context given when rendering a template. If you'd like to define your own variables, see the [Assignments](#assignments) section.

You can render a variable by using the `{{ name }}`.

Trying to access or render a variable that doesn't exist will result in an error.

A magical variable is available in every template if you want to print the current context: `__tera_context`.

#### Dot notation:
Construct and attributes can be accessed by using the dot (`.`) like `{{ product.name }}`.
Specific members of an array or tuple are accessed by using the `.i` notation, where i is a zero-based index. In dot notation variable can not be used after the dot (`.`).

#### Square bracket notation:
A more powerful alternative to (`.`) is to use square brackets (`[ ]`).
Variables can be rendered using the notation `{{product['name']}}` or `{{product["name"]}}`.

If the item is not in quotes it will be treated as a variable.
Assuming you have the following objects in your context `product = Product{ name: "Fred" }`
and `my_field = "name"`, calling `{{product[my_field]}}` will resolve to: `{{product.name}}`.

Only variables evaluating to string or integer number can be used as index: anything else will be
an error.

### Expressions

Tera allows expressions almost everywhere.

#### Math
You can do some basic math in Tera but it shouldn't be abused other than the occasional `+ 1` or similar.
Math operations are only allowed with numbers, using them on any other kind of values will result in an error.
You can use the following operators:

- `+`: adds 2 values together, `{{ 1 + 1 }}` will print `2`
- `-`: performs a subtraction, `{{ 2 - 1 }}` will print `1`
- `/`: performs a division, `{{ 10 / 2 }}` will print `5`
- `*`: performs a multiplication, `{{ 5 * 2 }}` will print `10`
- `%`: performs a modulo, `{{ 2 % 2 }}` will print `0`

The priority of operations is the following, from lowest to highest:

- `+` and `-`
- `*` and `/` and `%`


#### Comparisons

- `==`: checks whether the values are equal
- `!=`: checks whether the values are different
- `>=`: true if the left value is equal or greater to the right one
- `<=`: true if the right value is equal or greater to the left one
- `>`: true if the left value is greater than the right one
- `<`: true if the right value is greater than the left one

#### Logic

- `and`: true if the left and right operands are true
- `or`: true if the left or right operands are true
- `not`: negate an expression

#### Concatenation

You can concatenate several strings/numbers/idents using the `~` operator.

```jinja2
{{ "hello " ~ 'world' ~ `!` }}

{{ an_ident ~ " and a string" ~ another_ident }}

{{ an_ident ~ another_ident }}
```

An ident resolving to something other than a string or a number will raise an error.

#### `in` checking

You can check whether a left side is contained in a right side using the `in` operator.

Title: Tera Templates: Raw Blocks, Whitespace, Comments, Data Structures, and Expressions
Summary
This section explains how to use raw blocks, control whitespace, add comments, and work with data structures such as literals (booleans, numbers, strings, arrays) and variables in Tera templates. It also covers expressions, including math, comparisons, logic, concatenation, and the `in` operator.