Tera-driven CLI tool. Built on top of
tera
-, a powerful, easy-to-use template engine for Rust by Vincent Prouillet, for more details on Tera, please refer to https://keats.github.io/tera/
Just grab the executable, compiled for your operating system, from the Releases section.
You need just three arguments to pass to teraclio:
-s
,--source
path to your JSON document with data.-d
,--dest
path to the output produced by teraclio-t
,--template
the template to use to transform your data
teraclio --source <json-source> --dest <output-file> --template <template-path>
For example, when you need to collect a list of unresolved issues from SonarQube and generate a basic report out of it, you could do the following:
curl -H "Authorization: Bearer $SONARQUBE_TOKEN" "$SONARQUBE_URL/api/issues/search?componentKeys=$PROJECT_KEY&resolved=no" | jq '.issues[] | {key, message, severity, component, textRange}' | jq --arg REPO_NAME "$REPO_NAME" --arg PROJECT_KEY "$PROJECT_KEY" -s '{issues: ., repo: $REPO_NAME, project: $PROJECT_KEY}' >report.json
teraclio -s report.json -t template.txt -d report.txt
Where your template.txt
could look like this:
Issues report for {{data.repo}}
Project: {{get_env(name="PROJECT_NAME", default="")}}
{% for issue in data.issues %}
{{issue.component}}
{{issue.message}}
{% endfor %}
The content of your JSON document is available as data
root element. So if your JSON document having the structure like this, for example:
{
"issues": [],
"repo": "skitsanos/default-dashboard",
"pr": "200",
"project": "default-dashboard",
"createdOn": 1656023735372,
"updatedOn": 1656023735372
}
To access repo
property in your template, you would need to use data.repo
.
A Tera template is just a text file where variables and expressions are replaced with values when 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
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.
{% raw %}
Hello {{ name }}
{% endraw %}
would be rendered as Hello {{ name }}
.
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.
For example, let's look at the following template:
{% set my_var = 2 %}
{{ my_var }}
will have the following output:
2
If we want to get rid of the empty line, we can write the following:
{% set my_var = 2 -%}
{{ my_var }}
To comment out part of the template, wrap it in {# #}
. Anything in between those tags will not be rendered.
{# A comment #}
Tera has a few literals that can be used:
- booleans:
true
(orTrue
) andfalse
(orFalse
) - integers
- floats
- strings: text delimited by
""
,''
or ```` - arrays: a comma-separated list of literals and/or idents surrounded by
[
and]
(trailing comma allowed)
Variables are defined by the context given when rendering a template. If you'd like to define your own variables, see the Assignments section.
You can render a variable by using the {{ name }}
.
Trying to access or render a variable that doesn't exist will result in an error.
A magical variable is available in every template if you want to print the current context: __tera_context
.
Construct and attributes can be accessed by using the dot (.
) like {{ product.name }}
. Specific members of an array or tuple are accessed by using the .i
notation, where i is a zero-based index. In dot notation variable can not be used after the dot (.
).
A more powerful alternative to (.
) is to use square brackets ([ ]
). Variables can be rendered using the notation {{product['name']}}
or {{product["name"]}}
.
If the item is not in quotes it will be treated as a variable. Assuming you have the following objects in your context product = Product{ name: "Fred" }
and my_field = "name"
, calling {{product[my_field]}}
will resolve to: {{product.name}}
.
Only variables evaluating to string or integer number can be used as index: anything else will be an error.
Tera allows expressions almost everywhere.
You can do some basic math in Tera but it shouldn't be abused other than the occasional 1
or similar. Math operations are only allowed with numbers, using them on other values will result in an error. You can use the following operators:
{{ 1 1 }}
will print2
-
: performs a subtraction,{{ 2 - 1 }}
will print1
/
: performs a division,{{ 10 / 2 }}
will print5
*
: performs a multiplication,{{ 5 * 2 }}
will print10
%
: performs a modulo,{{ 2 % 2 }}
will print0
The priority of operations is the following, from lowest to highest:
-
*
and/
and%
==
: checks whether the values are equal!=
: checks whether the values are different>=
: true if the left value is equal or greater to the right one<=
: true if the right value is equal or greater to the left one>
: true if the left value is greater than the right one<
: true if the right value is greater than the left one
and
: true if the left and right operands are trueor
: true if the left or right operands are truenot
: negate an expression
You can concatenate several strings/numbers/idents using the ~
operator.
{{ "hello " ~ 'world' ~ `!` }}
{{ an_ident ~ " and a string" ~ another_ident }}
{{ an_ident ~ another_ident }}
An ident resolving to something other than a string or a number will raise an error.
You can check whether a left side is contained in a right side using the in
operator.
{{ some_var in [1, 2, 3] }}
{{ 'index' in page.path }}
{{ an_ident not in an_obj }}
Only literals/variables resulting in an array, a string and an object are supported in the right hand side: everything else will raise an error. While in the left hand side only literals/variables resulting in a number, a string and a boolean are supported.
You can assign values to variables during the rendering. Assignments in for loops and macros are scoped to their context but assignments outside of those will be set in the global context. Furthermore, assignments in for loop are valid until the end of the current iteration only.
{% set my_var = "hello" %}
{% set my_var = 1 4 %}
{% set my_var = some_var %}
{% set my_var = macros::some_macro() %}
{% set my_var = global_fn() %}
{% set my_var = [1, true, some_var | round] %}
If you want to assign a value in the global context while in a for loop, you can use set_global
:
{% set_global my_var = "hello" %}
{% set_global my_var = 1 4 %}
{% set_global my_var = some_var %}
{% set_global my_var = macros::some_macro() %}
{% set_global my_var = global_fn() %}
{% set_global my_var = [1, true, some_var | round] %}
Outside of a for loop, set_global
is exactly the same as set
.
You can modify variables using filters. Filters are separated from the variable by a pipe symbol (|
) and may have named arguments in parentheses. Multiple filters can be chained: the output of one filter is applied to the next.
For example, {{ name | lower | replace(from="doctor", to="Dr.") }}
will take a variable called name, make it lowercase and then replace instances of doctor
by Dr.
. It is equivalent to replace(lower(name), from="doctor", to="Dr.")
if we were to look at it as functions.
Calling filters on an incorrect type like trying to capitalize an array or using invalid types for arguments will result in an error.
Filters are functions with the fn(Value, HashMap<String, Value>) -> Result<Value>
definition and custom ones can be added like so:
tera.register_filter("upper", string::upper);
While filters can be used in math operations, they will have the lowest priority and therefore might not do what you expect:
{{ 1 a | length }}
// is equal to
{{ (1 a) | length } // this will probably error
// This will do what you wanted initially
{{ a | length 1 }}
Tera has many built-in filters that you can use.
Whole sections can also be processed by filters if they are encapsulated in {% filter name %}
and {% endfilter %}
tags where name
is the name of the filter:
{% filter upper %}
Hello
{% endfilter %}
This example transforms the text Hello
in all upper-case (HELLO
).
Filter sections can also contain block
sections like this:
{% filter upper %}
{% block content_to_be_upper_cased %}
This will be upper-cased
{% endblock content_to_be_upper_cased %}
{% endfilter %}
Conditionals are fully supported and are identical to the ones in Python.
{% 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:
{% if my_var %}
{{ my_var }}
{% else %}
Sorry, my_var isn't defined.
{% endif %}
Every if
statement has to end with an endif
tag.
Loop over items in a array:
{% for product in products %}
{{loop.index}}. {{product.name}}
{% endfor %}
Or on characters of a string:
{% 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-indexedloop.index0
: current iteration 0-indexedloop.first
: whether this is the first iterationloop.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:
{% 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:
{% for product in products | reverse %}
{{loop.index}}. {{product.name}}
{% endfor %}
You can also iterate on array literals:
{% for a in [1,2,3,] %}
{{a}}
{% endfor %}
Lastly, you can set a default body to be rendered when the container is empty:
{% for product in products %}
{{loop.index}}. {{product.name}}
{% else %}
No products.
{% endfor %}
Within a loop, break
and continue
may be used to control iteration.
To stop iterating when target_id
is reached:
{% for product in products %}
{% if product.id == target_id %}{% break %}{% endif %}
{{loop.index}}. {{product.name}}
{% endfor %}
To skip even-numbered items:
{% for product in products %}
{% if loop.index is even %}{% continue %}{% endif %}
{{loop.index}}. {{product.name}}
{% endfor %}
You can include a template to be rendered using the current context with the include
tag.
{% include "included.html" %}
The template path needs to be a static string. This is invalid:
{% 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.
{% 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.
{% include ["custom/header.html", "header.html"] %}
{% include ["special_sidebar.html", "sidebar.html"] ignore missing %}
Think of macros as functions or components that you can call and return some text.
They are defined as follows:
{% 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 value.
If a macro is defined in a separate file, you need to import the file containing the macros:
{% import "macros.html" as macros %}
You can name that file namespace (macros
in the example) anything you want. A macro is called like this:
// 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:
{% 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
.
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).
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:
<!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 %}
© 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.
Again, straight from Jinja2 docs:
{% 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.
Nested blocks also work in Tera. Consider the following templates:
// 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 inchild
andparent
template - It is in
child
so we render it, it contains asuper()
call so we render thehey
block fromparent
, which also contains asuper()
so we render thehey
block of thegrandparent
template as well - See
ending
block inchild
, render it and also render theending
block ofparent
as there is asuper()
The end result of that rendering (not counting whitespace) will be: "dad says hi and grandma says hello sincerely with love".
Tera has the following filters built-in:
Converts a string to lowercase.
Converts a string to uppercase.
Returns the number of words in a string.
Returns the string with all its characters lowercased apart from the first char which is uppercased.
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")}}
Adds slashes before quotes.
Example: {{ value | addslashes }}
If value is "I'm using Tera", the output will be "I'm using Tera".
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".
Capitalizes each word inside a sentence.
Example: {{ value | title }}
If value is "foo bar", the output will be "Foo Bar".
Removes leading and trailing whitespace if the variable is a string.
Removes leading whitespace if the variable is a string.
Removes trailing whitespace if the variable is a string.
Removes leading characters that match the given pattern if the variable is a string.
Example: {{ value | trim_start_matches(pat="//") }}
If value is "//a/b/c//", the output will be "a/b/c//".
Removes trailing characters that match the given pattern if the variable is a string.
Example: {{ value | trim_end_matches(pat="//") }}
If value is "//a/b/c//", the output will be "//a/b/c".
Truncates a string to the indicated length. If the string has a smaller length than the length
argument, the string is returned as is.
Example: {{ value | truncate(length=10) }}
By default, the filter will add an ellipsis at the end if the text was truncated. You can change the string appended by setting the end
argument. For example, {{ value | truncate(length=10, end="") }}
will not append anything.
Replaces line breaks (\n
or \r\n
) with HTML line breaks (<br>
).
Example: {{ value | linebreaksbr }}
If value is "Hello\r\nworld\n", the output will be "Hello
world
".
Note that if the template you are using it in is automatically escaped, you will need to call the safe
filter after linebreaksbr
.
Remove space (
) and line breaks (\n
or \r\n
) between HTML tags
Example: {{ value | spaceless }}
If the value is <p>\n<a> </a>\r\n </p>
, the output will be <p><a></a></p>
.
Note that only whitespace between successive opening tags and successive closing tags is removed.
Also note that if the template you are using it in is automatically escaped, you will need to call the safe
filter after spaceless
.
Indents a string by injecting a prefix at the start of each line. The prefix
argument (default 4 spaces) specifies the prefix to insert per line. If the first
argument (default false) is set true spaces are inserted for the first line. If the blank
argument (default false) is set true spaces are inserted for blank/whitespace lines.
Tries to remove HTML tags from input. Does not guarantee well formed output if input is not valid HTML.
Example: {{ value | striptags }}
If value is "Joel", the output will be "Joel".
Note that if the template you are using it in is automatically escaped, you will need to call the safe
filter after striptags
.
Returns the first element of an array. If the array is empty, returns empty string.
Returns the last element of an array. If the array is empty, returns empty string.
Returns the nth element of an array.§ If the array is empty, returns empty string. It takes a required n
argument, corresponding to the 0-based index you want to get.
Example: {{ value | nth(n=2) }}
Joins an array with a string.
Example: {{ value | join(sep=" // ") }}
If value is the array ['a', 'b', 'c']
, the output will be the string "a // b // c".
Returns the length of an array, an object, or a string.
Returns a reversed string or array.
Sorts an array into ascending order.
The values in the array must be a sortable type:
- numbers are sorted by their numerical value.
- strings are sorted in alphabetical order.
- arrays are sorted by their length.
- bools are sorted as if false=0 and true=1
If you need to sort a list of structs or tuples, use the attribute
argument to specify which field to sort by.
Example:
Given people
is an array of Person
struct Name(String, String);
struct Person {
name: Name,
age: u32,
}
The attribute
argument can be used to sort by last name:
{{ people | sort(attribute="name.1") }}
or by age:
{{ people | sort(attribute="age") }}
Removes duplicate items from an array. The attribute
argument can be used to select items based on the values of an inner attribute. For strings, the case_sensitive
argument (default is false) can be used to control the comparison.
Example:
Given people
is an array of Person
struct Name(String, String);
struct Person {
name: Name,
age: u32,
}
The attribute
argument can be used to select one Person for each age:
{{ people | unique(attribute="age") }}
or by last name:
{{ people | unique(attribute="name.1", case_sensitive="true") }}
Slices an array by the given start
and end
parameter. Both parameters are optional and omitting them will return the same array. Use the start
argument to define where to start (inclusive, default to 0
) and end
argument to define where to stop (exclusive, default to the length of the array). start
and end
are 0-indexed.
{% for i in my_arr | slice(end=5) %}
{% for i in my_arr | slice(start=1) %}
{% for i in my_arr | slice(start=1, end=5) %}
You can also use negative index values to refer the array from the last element. -1 refers to the last index, -2 refers to the second last index and so on.
For example, let's look at the following template:
{% for i in my_arr | slice(end=-2) %}
will produce the follow output for my_array = [1, 2, 3, 4, 5]
: [1, 2, 3]
Groups an array using the required attribute
argument. The filter takes an array and returns a map where the keys are the values of the attribute
stringified and the values are all elements of the initial array having that attribute
. Values with missing attribute
or where attribute
is null will be discarded.
Example:
Given posts
is an array of Post
struct Author {
name: String,
};
struct Post {
content: String,
year: u32,
author: Author,
}
The attribute
argument can be used to group posts by year:
{{ posts | group_by(attribute="year") }}
or by author name:
{% for name, author_posts in posts | group_by(attribute="author.name") %}
{{ name }}
{% for post in author_posts %}
{{ post.year }}: {{ post.content }}
{% endfor %}
{% endfor %}
Manipulating the hashmap produced by group_by
in an arbitrary order requires additional steps to extract the keys into a separate array.
Example:
{% set map = section.pages | group_by(attribute="year") %}
{% set_global years = [] %}
{% for year, ignored in map %}
{% set_global years = years | concat(with=year) %}
{% endfor %}
{% for year in years | reverse %}
{% set posts = map[year] %}
{% endfor %}
Filters the array values, returning only the values where the attribute
is equal to the value
. Values with missing attribute
or where attribute
is null will be discarded.
attribute
is mandatory.
Example:
Given posts
is an array of Post
struct Author {
name: String,
};
struct Post {
content: String,
year: u32,
author: Author,
draft: bool,
}
The attribute
argument can be used to filter posts by draft value:
{{ posts | filter(attribute="draft", value=true) }}
or by author name:
{{ posts | filter(attribute="author.name", value="Vincent") }}
If value
is not passed, it will drop any elements where the attribute is null
.
Retrieves an attribute from each object in an array. The attribute
argument is mandatory and specifies what to extract.
Example:
Given people
is an array of Person
struct Name(String, String);
struct Person {
name: Name,
age: u32,
}
The attribute
argument is used to retrieve their ages.
{{ people | map(attribute="age") }}
Appends values to an array.
{{ posts | concat(with=drafts) }}
The filter takes an array and returns a new array with the value(s) from the with
parameter added. If the with
parameter is an array, all of its values will be appended one by one to the new array and not as an array.
This filter can also be used to append a single value to an array if the value passed to with
is not an array:
{% set pages_id = pages_id | concat(with=id) %}
The with
attribute is mandatory.
Percent-encodes all the characters in a string which are not included in unreserved chars (according to RFC3986) with the exception of forward slash (/
).
Example: {{ value | urlencode }}
If value is /foo?a=b&c=d
, the output will be /foo?a=b&c=d
. /
is not escaped.
Similar to urlencode
filter but encodes all non-alphanumeric characters in a string including forward slashes (/
).
Example: {{ value | urlencode_strict }}
If value is /foo?a=b&c=d
, the output will be /foo?a=b&c=d
. /
is also encoded.
Returns the absolute value
Example: {{ negative_number | abs }}
If negative_number is -1, the output will be 1. If num_messages is -2.0 the output will be 2.
Returns a plural suffix if the value is not equal to ±1, or a singular suffix otherwise. The plural suffix defaults to s
and the singular suffix defaults to the empty string (i.e. nothing).
Example: You have {{ num_messages }} message{{ num_messages | pluralize }}
If num_messages is 1, the output will be You have 1 message. If num_messages is 2 the output will be You have 2 messages. You can also customize the singular and plural suffixes with the singular
and plural
arguments to the filter:
Example: {{ num_categories }} categor{{ num_categories | pluralize(singular="y", plural="ies") }}
Returns a number rounded following the method given. Default method is common
which will round to the nearest integer. ceil
and floor
are available as alternative methods. Another optional argument, precision
, is available to select the precision of the rounding. It defaults to 0
, which will round to the nearest integer for the given method.
Example: {{ num | round }} {{ num | round(method="ceil", precision=2) }}
Returns a human-readable file size (i.e. '110 MB') from an integer.
Example: {{ num | filesizeformat }}
Parses a timestamp into a date(time) string. Defaults to YYYY-MM-DD
format. Time formatting syntax is inspired from strftime and a full reference is available on chrono docs.
Example: {{ ts | date }} {{ ts | date(format="%Y-%m-%d %H:%M") }}
If you are using ISO 8601 date strings or a UTC timestamp, you can optionally supply a timezone for the date to be rendered in.
Example:
{{ "2019-09-19T13:18:48.731Z" | date(timezone="America/New_York") }}
{{ "2019-09-19T13:18:48.731Z" | date(format="%Y-%m-%d %H:%M", timezone="Asia/Shanghai") }}
{{ 1648252203 | date(timezone="Europe/Berlin") }}
Locale can be specified (excepted when the input is a timestamp without timezone argument), default being POSIX.
Example: {{ 1648252203 | date(format="%A %-d %B", timezone="Europe/Paris", locale="fr_FR") }}
Escapes a string's HTML. Specifically, it makes these replacements:
&
is converted to&
<
is converted to<
>
is converted to>
"
(double quote) is converted to"
'
(single quote) is converted to'
/
is converted to/
Escapes XML special characters. Specifically, it makes these replacements:
&
is converted to&
<
is converted to<
>
is converted to>
"
(double quote) is converted to"
'
(single quote) is converted to'
Marks a variable as safe: HTML will not be escaped anymore. safe
only works if it is the last filter of the expression:
{{ content | replace(from="Robert", to="Bob") | safe }}
will not be escaped{{ content | safe | replace(from="Robert", to="Bob") }}
will be escaped
Accesses a value from an object when the key is not a Tera identifier. Example: {{ sections | get(key="posts/content") }}
The get
filter also has a default
parameter which can be used to provide a return value when the key
parameter is missing from the set being filtered. Example: {{ sections | get(key="posts/content", default="default") }}
Splits a string into an array of strings, separated by a pattern given. Example: {{ path | split(pat="/") }}
Converts a value into an integer. The default
argument can be used to specify the value to return on error, and the base
argument can be used to specify how to interpret the number. Bases of 2, 8, and 16 understand the prefix 0b, 0o, 0x, respectively.
Converts a value into a float. The default
argument can be used to specify the value to return on error.
Transforms any value into a JSON representation. This filter is better used together with safe
or when automatic escape is disabled.
Example: {{ value | json_encode() | safe }}
It accepts a parameter pretty
(boolean) to print a formatted JSON instead of a one-liner.
Example: {{ value | json_encode(pretty=true) | safe }}
Returns a string representation of the given value.
Example: {{ value | as_str }}
Returns the default value given only if the variable evaluated is not present in the context and is therefore meant to be at the beginning of a filter chain if there are several filters.
Example: {{ value | default(value=1) }}
This is in most cases a shortcut for:
{% if value %}{{ value }}{% else %}1{% endif %}
However, only the existence of the value in the context is checked. With a value that if
would evaluate to false (such as an empty string, or the number 0), the default
filter will not attempt replace it with the alternate value provided. For example, the following will produce "I would like to read more !":
I would like to read more {{ "" | default (value="Louise Michel") }}!
If you intend to use the default filter to deal with optional values, you should make sure those values aren't set! Otherwise, use a full if
block. This is especially relevant for dealing with optional arguments passed to a macro.
Here are the currently built-in tests:
Returns true if the given variable is defined.
Returns true if the given variable is undefined.
Returns true if the given variable is an odd number.
Returns true if the given variable is an even number.
Returns true if the given variable is a string.
Returns true if the given variable is a number.
Returns true if the given expression is divisible by the arg given.
Example:
{% if rating is divisibleby(2) %}
Divisible
{% endif %}
Returns true if the given variable can be iterated over in Tera (i.e. is an array/tuple or an object).
Returns true if the given variable is an object (i.e. can be iterated over key, value).
Returns true if the given variable is a string and starts with the arg given.
Example:
{% if path is starting_with("x/") %}
In section x
{% endif %}
Returns true if the given variable is a string and ends with the arg given.
Returns true if the given variable contains the arg given.
The test works on:
- strings: is the arg a substring?
- arrays: is the arg given one of the members of the array?
- maps: is the arg given a key of the map?
Example:
{% if username is containing("xXx") %}
Bad
{% endif %}
Returns true if the given variable is a string and matches the regex in the argument.
Example:
{% if name is matching("^[Qq]ueen") %}
Her Royal Highness, {{ name }}
{% elif name is matching("^[Kk]ing") %}
His Royal Highness, {{ name }}
{% else %}
{{ name }}
{% endif %}
A comprehensive syntax description can be found in the regex crate documentation.
Tera comes with some built-in global functions.
Returns an array of integers created using the arguments given. There are 3 arguments, all integers:
end
: stop beforeend
, mandatorystart
: where to start from, defaults to0
step_by
: with what number do we increment, defaults to1
Returns the local datetime as string or the timestamp as integer if requested.
There are 2 arguments, both booleans:
timestamp
: whether to return the timestamp instead of the datetimeutc
: whether to return the UTC datetime instead of the local one
Formatting is not built-in the global function but you can use the date
filter like so now() | date(format="%Y")
if you wanted to get the current year.
The template rendering will error with the given message when encountered.
There is only one string argument:
message
: the message to display as the error
Returns a random integer in the given range. There are 2 arguments, both integers:
start
: defaults to 0 if not presentend
: required
start
is inclusive (i.e. can be returned) and end
is exclusive.
Returns the environment variable value for the name given. It will error if the environment variable is not found but the call can also take a default value instead.
name
: the name of the environment variable to look for, requireddefault
: a default value in case the environment variable is not found
If the environment variable is found, it will always be a string while your default could be of any type.