Tera

Tera

Tera is a template engine for Rust inspired by Jinja2 and the Django template language.

Usage

Basic example:

<title>{% block title %}{% endblock title %}</title>
<ul>
{% for user in users -%}
  <li><a href="{{ user.url }}">{{ user.username }}</a></li>
{%- endfor %}
</ul>
  • Hot reloading: tera.full_reload()?;
  • Loading templates from strings: tera.add_raw_template("hello.html", "the body")?;
  • Render a one off template: Tera::one_off(user_tpl, context, true);

Built-in

Variables

  • __tera_context: Magical variable is available in every template if you want to print the current context
{{ __tera_context }}

Filters

  • safe: Marks a variable as safe: HTML will not be escaped anymore.
<h1>Hello {{ user.name | safe }} !</h1>

Functions

  • get_env: Returns the environment variable value for the name given.
{{ get_env(name="SECRET") }} # Leak environment variables

References