Home Explore Blog CI



tera

8th chunk of `docs/content/docs/_index.md`
92f5c2d7ea2b1f56d0d432b43c151b799db69a8c2c0ceb090000000100000fa4
<body>
    <div id="content">{% block content %}{% endblock content %}</div>
    <div id="footer">
        {% block footer %}
        &copy; Copyright 2008 by <a href="http://domain.invalid/">you</a>.
        {% endblock footer %}
    </div>
</body>
</html>
```

This `base.html` template defines 4 `block` tags that child templates can override.
The `head` and `footer` block have some content already which will be rendered if they are not overridden.

### Child template
Again, straight from Jinja2 docs:

```jinja2
{% extends "base.html" %}
{% block title %}Index{% endblock title %}
{% block head %}
    {{/* super() */}}
    <style type="text/css">
        .important { color: #336699; }
    </style>
{% endblock head %}
{% block content %}
    <h1>Index</h1>
    <p class="important">
      Welcome to my awesome homepage.
    </p>
{% endblock content %}
```

To indicate inheritance, you have to use the `extends` tag as the first thing in the file followed by the name of the template you want
to extend.
The `{{/* super() */}}` variable call tells Tera to render the parent block there.

Please note that in a child template, any content outside of a block will be ignored, including variable assignments.

Nested blocks also work in Tera. Consider the following templates:

```jinja2
// grandparent
{% block hey %}hello{% endblock hey %}

// parent
{% extends "grandparent" %}
{% block hey %}hi and grandma says {{/* super() */}} {% block ending %}sincerely{% endblock ending %}{% endblock hey %}

// child
{% extends "parent" %}
{% block hey %}dad says {{/* super() */}}{% endblock hey %}
{% block ending %}{{/* super() */}} with love{% endblock ending %}
```
The block `ending` is nested in the `hey` block. Rendering the `child` template will do the following:

- Find the first base template: `grandparent`
- See `hey` block in it and check if it is in `child` and `parent` template
- It is in `child` so we render it, it contains a `super()` call so we render the `hey` block from `parent`,
which also contains a `super()` so we render the `hey` block of the `grandparent` template as well
- See `ending` block in `child`, render it and also render the `ending` block of `parent` as there is a `super()`

The end result of that rendering (not counting whitespace) will be: "dad says hi and grandma says hello sincerely with love".

This example explicitly terminates named blocks with `{% endblock hey %}`. It's not required to give the name of the block 
being terminated `{% endblock %}`, though it may add some clarity.

See the note in the [Include](@/docs/_index.md#include) section regarding mixing inheritance and includes.

## Built-ins

### Built-in filters

Tera has the following filters built-in:

#### lower
Converts a string to lowercase.

#### upper
Converts a string to uppercase.

#### wordcount
Returns the number of words in a string.

#### capitalize
Returns the string with all its characters lowercased apart from the first char which is uppercased.

#### replace
Takes 2 mandatory string named arguments: `from` and `to`. It will return a string with all instances of
the `from` string replaced with the `to` string.

Example: `{{ name | replace(from="Robert", to="Bob")}}`

#### addslashes
Adds slashes before quotes.

Example: `{{ value | addslashes }}`

If value is "I'm using Tera", the output will be "I\\'m using Tera".

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

Transforms a string into ASCII, lowercases it, trims it, converts spaces to hyphens and
removes all characters that are not numbers, lowercase letters or hyphens.

Example: `{{ value | slugify }}`

If value is "-Hello world! ", the output will be "hello-world".

#### title
Capitalizes each word inside a sentence.

Example: `{{ value | title }}`

If value is "foo  bar", the output will be "Foo  Bar".

#### trim
Removes leading and trailing whitespace if the variable is a string.

#### trim_start
Removes leading whitespace if the variable is a string.

#### trim_end

Title: Tera Templates: Inheritance Details and Built-in Filters
Summary
This section delves deeper into inheritance in Tera templates, explaining how to use the `extends` tag, the `super()` function to render parent blocks, and how nested blocks function. It also emphasizes that content outside blocks in a child template is ignored. Then, it lists and describes Tera's built-in filters, including `lower`, `upper`, `wordcount`, `capitalize`, `replace`, `addslashes`, `slugify`, `title`, `trim`, `trim_start` and `trim_end`.