Home Explore Blog CI



tera

6th chunk of `docs/content/docs/_index.md`
e91dd736d9c62e56fff53f08dd9a1bebc85a186819bcd8ab0000000100000fb8
- variable block: `{{/* url_for(name="home") */}}`
- for loop container: `{% for i in range(end=5) %}`

Tera comes with some [built-in functions](@/docs/_index.md#built-in-functions).

## Control structures

### If

Conditionals are fully supported and are identical to the ones in Python.

```jinja2
{% if price < 10 or always_show %}
   Price is {{ price }}.
{% elif price > 1000 and not rich %}
   That's expensive!
{% else %}
    N/A
{% endif %}
```

Undefined variables are considered falsy. This means that you can test for the
presence of a variable in the current context by writing:

```jinja2
{% if my_var %}
    {{ my_var }}
{% else %}
    Sorry, my_var isn't defined.
{% endif %}
```
Every `if` statement has to end with an `endif` tag.

### For

Loop over items in a array:
```jinja2
{% for product in products %}
  {{loop.index}}. {{product.name}}
{% endfor %}
```

Or on characters of a string:

```jinja2
{% for letter in name %}
  {% if loop.index % 2 == 0%}
    <span style="color:red">{{ letter }}</span>
  {% else %}
    <span style="color:blue">{{ letter }}</span>
  {% endif %}
{% endfor %}
```

A few special variables are available inside for loops:

- `loop.index`: current iteration 1-indexed
- `loop.index0`: current iteration 0-indexed
- `loop.first`: whether this is the first iteration
- `loop.last`: whether this is the last iteration

Every `for` statement has to end with an `endfor` tag.

You can also loop on maps and structs using the following syntax:
```jinja2
{% for key, value in products %}
  {{key}}. {{value.name}}
{% endfor %}
```
`key` and `value` can be named however you want, they just need to be separated with a comma.

If you are iterating on an array, you can also apply filters to the container:

```jinja2
{% for product in products | reverse %}
  {{loop.index}}. {{product.name}}
{% endfor %}
```

You can also iterate on array literals:

```jinja2
{% for a in [1,2,3,] %}
  {{a}}
{% endfor %}
```

Lastly, you can set a default body to be rendered when the container is empty:


```jinja2
{% for product in products %}
  {{loop.index}}. {{product.name}}
{% else %}
  No products.  
{% endfor %}
```

#### Loop Controls

Within a loop, `break` and `continue` may be used to control iteration.

To stop iterating when `target_id` is reached:

```jinja2
{% for product in products %}
  {% if product.id == target_id %}{% break %}{% endif %}
  {{loop.index}}. {{product.name}}
{% endfor %}
```

To skip even-numbered items:
```jinja2
{% for product in products %}
  {% if loop.index is even %}{% continue %}{% endif %}
  {{loop.index}}. {{product.name}}
{% endfor %}
```

### Include

You can include a template to be rendered using the current context with the `include` tag.

```jinja
{% include "included.html" %}
```

The template path needs to be a static string. This is invalid:

```jinja
{% include "partials/" ~ name ~ ".html" %}
```

Tera doesn't offer passing a custom context to the `include` tag.
If you want to do that, use macros.

While you can `set` values in included templates, those values only exist while rendering
them: the template calling `include` doesn't see them.

You can mark an include with `ignore missing`, so that Tera will ignore the statement if the template to be included does not exist.

```jinja2
{% include "header.html" ignore missing %}
```

You can also provide a list of templates that are checked for existence before inclusion. The first template that exists will be included. If `ignore missing` is given, it will fall back to rendering nothing if none of the templates exist.

```jinja2
{% include ["custom/header.html", "header.html"] %}
{% include ["special_sidebar.html", "sidebar.html"] ignore missing %}
```

Note: `include` works similar to how it does in other engines like Jinja, with the exception that the current version of Tera doesn't allow inheritance within included files. Practically
speaking this means you have to choose between using `include`s or `extends` to organise your site, without mixing them. 

Title: Tera Template Control Structures: If, For, Include
Summary
This section details the control structures available in Tera templates, including `if` statements for conditionals, `for` loops for iterating over arrays, strings, maps, and structs, and the `include` tag for embedding other templates. It covers loop-specific variables like `loop.index` and loop controls such as `break` and `continue`. It also mentions limitations of the `include` tag regarding custom contexts and inheritance.