Home Explore Blog CI



tera

2nd chunk of `docs/content/docs/_index.md`
ffe1a483ee431d05e4c27a4242d1f006cb487d8630205a860000000100000fa8
You can override that or completely disable auto-escaping by calling the `autoescape_on` method:

```rs
// escape only files ending with `.php.html`
tera.autoescape_on(vec![".php.html"]);
// disable autoescaping completely
tera.autoescape_on(vec![]);
```

Tera does not perform contextual auto-escaping, e.g. by parsing the template to know whether to escape JS, CSS or HTML (see 
<https://rawgit.com/mikesamuel/sanitized-jquery-templates/trunk/safetemplate.html> for more details on that).

## Advanced usage

### Extending another instance
If you are using a framework or a library using Tera, chances are they provide their own Tera instance with some
built-in templates, filters, global functions or testers. Tera offers an `extend` method that will extend your own
instance with everything mentioned before:

```rs
let mut tera = Tera::new(&tpl_glob).chain_err(|| "Error parsing templates")?;
// ZOLA_TERA is an instance present in a library
tera.extend(&ZOLA_TERA)?;
```
If anything - templates, filters, etc - with the same name exists in both instances, Tera will only keep yours.

### Reloading
If you are watching a directory and want to reload templates on change (editing/adding/removing a template), Tera gives
the `full_reload` method:

```rs
tera.full_reload()?;
```

Note that reloading is only available if you are loading templates with a glob.

### Loading templates from strings
Tera allows you to load templates not only from files but also from plain strings.

```rs
// one template only
let mut tera = Tera::default();
tera.add_raw_template("hello.html", "the body")?;

// many templates
let mut tera = Tera::default();
tera.add_raw_templates(vec![
    ("grandparent", "{% block hey %}hello{% endblock hey %}"),
    ("parent", "{% extends \"grandparent\" %}{% block hey %}Parent{% endblock hey %}"),
])?;
```
If some templates are related, for example one extending the other, you will need to use the `add_raw_templates` method
as Tera will error if it find inconsistencies such as extending a template that Tera doesn't know about.

### Render a one off template

Want to render a single template, for example one coming from a user? The `one_off` function is there for that.

```rs
// The last parameter is whether we want to autoescape the template or not.
// Should be true in 99% of the cases for HTML
let context = Context::new();
// add stuff to context
let result = Tera::one_off(user_tpl, context, true);
```


# Templates

## Introduction

### Tera Basics

A Tera template is just a text file where variables and expressions get replaced with values
when it is rendered. The syntax is based on Jinja2 and Django templates.

There are 3 kinds of delimiters and those cannot be changed:

- `{{` and `}}` for expressions
- `{%` and `%}` for statements
- `{#` and `#}` for comments

### Raw

Tera will consider all text inside the `raw` block as a string and won't try to
render what's inside. Useful if you have text that contains Tera delimiters.

```jinja2
{% raw %}
  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 ` `` `

Title: Advanced Tera Usage: Reloading, String Templates, and Template Basics
Summary
This section covers advanced features of Tera, including reloading templates on file changes, loading templates from strings, and rendering one-off templates. It introduces Tera template basics, such as delimiters, raw blocks, whitespace control, comments, and data structures like literals (booleans, integers, floats, and strings).