Home Explore Blog CI



tera

7th chunk of `docs/content/docs/_index.md`
7b481648477fc501d299a1b4e2d792cd43d02e1b01a14b59000000010000101d
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. 

### Macros

Think of macros as functions or components that you can call and return some text.

They are defined as follows:

```jinja2
{% macro input(label, type="text") %}
    <label>
        {{ label }}
        <input type="{{type}}" />
    </label>
{% endmacro input %}
```
As shown in the example above, macro arguments can have a default [literal](@/docs/_index.md#literals) value.

If a macro is defined in a separate file, you need to import the file containing the macros:

```jinja2
{% import "macros.html" as macros %}
```
You can name that file namespace (`macros` in the example) anything you want.
A macro is called like this:

```jinja2
// namespace::macro_name(**kwargs)
{{ macros::input(label="Name", type="text") }}
```
Do note that macros, like filters, require keyword arguments.
Use the `self` namespace when calling a macro defined in the same file. Macros must be defined top-level (they cannot be nested in an if, for, etc.) and should only reference arguments, not template variables directly.


Macros can be called recursively but there is no limit to recursion so make sure your macro ends.

Here's an example of a recursive macro:

```jinja2
{% macro factorial(n) %}
  {% if n > 1 %}{{ n }} - {{ self::factorial(n=n-1) }}{% else %}1{% endif %}
{% endmacro factorial %}
```

A macro's body can contain all normal Tera syntax with the exception of macros definition, `block` and `extends`.


## Inheritance

Tera uses the same kind of inheritance as Jinja2 and Django templates:
you define a base template and extend it in child templates through blocks.
There can be multiple levels of inheritance (i.e. A extends B that extends C).

### Base template
A base template typically contains the basic document structure as well as
several `blocks` that can have content.

For example, here's a `base.html` almost copied from the Jinja2 documentation:

```jinja2
<!DOCTYPE html>
<html lang="en">
<head>
    {% block head %}
    <link rel="stylesheet" href="style.css" />
    <title>{% block title %}{% endblock title %} - My Webpage</title>
    {% endblock head %}
</head>
<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

Title: Tera Templates: Macros and Inheritance
Summary
This section covers Macros and Inheritance in Tera templates. Macros are reusable code snippets, similar to functions, that return text and can be defined with default argument values. Macros can be imported from other files and called with keyword arguments. Inheritance in Tera, similar to Jinja2 and Django, involves base templates with blocks that child templates can override using the `extends` tag. The include tag also supports 'ignore missing' which lets Tera ignore the include statement if the template does not exist and also supports including a list of templates and it will render the first template that exists.