2017-09-27 14:37:17 +00:00
+++
title = "Overview"
weight = 10
+++
2019-11-26 19:30:30 +00:00
Zola uses the [Tera ](https://tera.netlify.com ) template engine, which is very similar
to Jinja2, Liquid and Twig.
2017-10-19 11:48:50 +00:00
2018-10-18 21:09:32 +00:00
As this documentation will only talk about how templates work in Zola, please read
2019-12-10 06:00:58 +00:00
the [Tera template documentation ](https://tera.netlify.com/docs#templates ) if you want
2017-10-19 11:48:50 +00:00
to learn more about it first.
2017-09-27 14:37:17 +00:00
2019-11-26 19:30:30 +00:00
All templates live in the `templates` directory. If you are not sure what variables are available in a template,
you can place `{{ __tera_context }}` in the template to print the whole context.
2017-09-27 14:37:17 +00:00
2019-11-26 19:30:30 +00:00
A few variables are available on all templates except RSS and the sitemap:
2017-09-27 14:37:17 +00:00
2019-05-27 12:35:14 +00:00
- `config` : the [configuration ](@/documentation/getting-started/configuration.md ) without any modifications
2019-11-26 19:30:30 +00:00
- `current_path` : the path (full URL without `base_url` ) of the current page, never starting with a `/`
- `current_url` : the full URL for the current page
- `lang` : the language for the current page; `null` if the page/section doesn't have a language set
2017-09-27 14:37:17 +00:00
2019-11-26 19:30:30 +00:00
The 404 template does not get `current_path` and `current_url` (this information cannot be determined).
2019-05-01 21:06:02 +00:00
2019-11-26 19:30:30 +00:00
## Standard templates
2018-10-18 21:09:32 +00:00
By default, Zola will look for three templates: `index.html` , which is applied
2018-07-31 14:35:16 +00:00
to the site homepage; `section.html` , which is applied to all sections (any HTML
page generated by creating a directory within your `content` directory); and
2019-11-26 19:30:30 +00:00
`page.html` , which is applied to all pages (any HTML page generated by creating an
2018-07-16 06:49:26 +00:00
`.md` file within your `content` directory).
The homepage is always a section (regardless of whether it contains other pages).
2018-07-31 14:35:16 +00:00
Thus, the `index.html` and `section.html` templates both have access to the
2018-07-16 06:49:26 +00:00
section variables. The `page.html` template has access to the page variables.
2019-11-26 19:30:30 +00:00
The page and section variables are described in more detail in the next section.
2018-07-16 06:49:26 +00:00
2019-11-26 19:30:30 +00:00
## Built-in templates
Zola comes with three built-in templates: `rss.xml` , `sitemap.xml` and
`robots.txt` (each is described in its own section of this documentation).
2018-07-31 14:35:16 +00:00
Additionally, themes can add their own templates, which will be applied if not
2018-07-16 06:49:26 +00:00
overridden. You can override built-in or theme templates by creating a template with
2019-11-26 19:30:30 +00:00
the same name in the correct path. For example, you can override the RSS template by
2018-07-16 06:49:26 +00:00
creating a `templates/rss.xml` file.
2019-11-26 19:30:30 +00:00
## Custom templates
In addition to the standard `index.html` , `section.html` and `page.html` templates,
you may also create custom templates by creating an `.html` file in the `templates`
directory. These custom templates will not be used by default. Instead, a custom template will _only_ be used if you apply it by setting the `template` front-matter variable to the path for that template (or if you `include` it in another template that is applied). For example, if you created a custom template for your site's About page called `about.html` , you could apply it to your `about.md` page by including the following front matter in your `about.md` page:
2018-07-16 06:49:26 +00:00
```md
+++
title = "About Us"
template = "about.html"
+++
```
Custom templates are not required to live at the root of your `templates` directory.
For example, `product_pages/with_pictures.html` is a valid template.
2017-09-27 14:37:17 +00:00
## Built-in filters
2020-03-09 07:15:18 +00:00
Zola adds a few filters in addition to [those ](https://tera.netlify.com/docs/#filters ) already present
2019-11-26 19:30:30 +00:00
in Tera.
2017-09-27 14:37:17 +00:00
### markdown
Converts the given variable to HTML using Markdown. This doesn't apply any of the
2019-11-26 19:30:30 +00:00
features that Zola adds to Markdown; for example, internal links and shortcodes won't work.
2017-09-27 14:37:17 +00:00
2019-11-26 19:30:30 +00:00
By default, the filter will wrap all text in a paragraph. To disable this behaviour, you can
2018-01-26 15:22:37 +00:00
pass `true` to the inline argument:
```jinja2
{{ some_text | markdown(inline=true) }}
```
2017-09-27 14:37:17 +00:00
### base64_encode
Encode the variable to base64.
### base64_decode
Decode the variable from base64.
## Built-in global functions
2018-11-07 18:45:03 +00:00
2019-11-26 19:30:30 +00:00
Zola adds a few global functions to [those in Tera ](https://tera.netlify.com/docs/templates/#built-in-functions )
to make it easier to develop complex sites.
2018-11-07 18:45:03 +00:00
2017-09-27 14:37:17 +00:00
### `get_page`
2019-11-26 19:30:30 +00:00
Takes a path to an `.md` file and returns the associated page.
2017-09-27 14:37:17 +00:00
```jinja2
{% set page = get_page(path="blog/page2.md") %}
```
### `get_section`
2019-11-26 19:30:30 +00:00
Takes a path to an `_index.md` file and returns the associated section.
2017-09-27 14:37:17 +00:00
```jinja2
2018-01-29 17:40:12 +00:00
{% set section = get_section(path="blog/_index.md") %}
2017-09-27 14:37:17 +00:00
```
2018-10-18 14:49:33 +00:00
If you only need the metadata of the section, you can pass `metadata_only=true` to the function:
```jinja2
{% set section = get_section(path="blog/_index.md", metadata_only=true) %}
```
2017-10-22 11:16:25 +00:00
### ` get_url`
2017-09-27 14:37:17 +00:00
Gets the permalink for the given path.
2019-11-26 19:30:30 +00:00
If the path starts with `@/` , it will be treated as an internal
link like the ones used in Markdown, starting from the root `content` directory.
2017-09-27 14:37:17 +00:00
```jinja2
2019-05-27 12:35:14 +00:00
{% set url = get_url(path="@/blog/_index.md") %}
2017-09-27 14:37:17 +00:00
```
2019-11-26 19:30:30 +00:00
This can also be used to get the permalinks for static assets, for example if
2017-09-27 14:37:17 +00:00
we want to link to the file that is located at `static/css/app.css` :
```jinja2
2018-05-17 16:32:46 +00:00
{{/* get_url(path="css/app.css") */}}
2017-09-27 14:37:17 +00:00
```
2018-10-18 14:49:33 +00:00
By default, assets will not have a trailing slash. You can force one by passing `trailing_slash=true` to the `get_url` function.
An example is:
2017-11-10 16:46:14 +00:00
```jinja2
2018-10-18 14:49:33 +00:00
{{/* get_url(path="css/app.css", trailing_slash=true) */}}
2017-11-10 16:46:14 +00:00
```
2017-09-27 14:37:17 +00:00
In the case of non-internal links, you can also add a cachebust of the format `?t=1290192` at the end of a URL
by passing `cachebust=true` to the `get_url` function.
2017-11-16 17:08:06 +00:00
2019-05-30 18:06:24 +00:00
### `get_image_metadata`
2019-11-26 19:30:30 +00:00
Gets metadata for an image. Currently, the only supported keys are `width` and `height` .
2019-05-30 18:06:24 +00:00
```jinja2
{% set meta = get_image_metadata(path="...") %}
Our image is {{ meta.width }}x{{ meta.height }}
```
2018-01-16 12:57:31 +00:00
### `get_taxonomy_url`
2018-07-31 14:35:16 +00:00
Gets the permalink for the taxonomy item found.
2017-11-16 17:08:06 +00:00
```jinja2
2018-07-31 14:35:16 +00:00
{% set url = get_taxonomy_url(kind="categories", name=page.taxonomies.category) %}
2017-11-16 17:08:06 +00:00
```
2019-11-26 19:30:30 +00:00
`name` will almost always come from a variable but in case you want to do it manually,
the value should be the same as the one in the front matter, not the slugified version.
2018-01-16 12:57:31 +00:00
2018-07-31 14:35:16 +00:00
### `get_taxonomy`
Gets the whole taxonomy of a specific kind.
```jinja2
2019-03-25 22:15:51 +00:00
{% set categories = get_taxonomy(kind="categories") %}
2018-07-31 14:35:16 +00:00
```
2018-10-18 15:32:30 +00:00
### `load_data`
2019-03-25 22:15:51 +00:00
Loads data from a file or URL. Supported file types include *toml* , *json* and *csv* .
2019-03-16 09:01:11 +00:00
Any other file type will be loaded as plain text.
2018-10-18 15:32:30 +00:00
2019-03-16 09:01:11 +00:00
The `path` argument specifies the path to the data file relative to your base directory, where your `config.toml` is.
2019-11-26 19:30:30 +00:00
As a security precaution, if this file is outside the main site directory, your site will fail to build.
2018-10-18 15:32:30 +00:00
```jinja2
2019-03-16 09:01:11 +00:00
{% set data = load_data(path="content/blog/story/data.toml") %}
2018-10-18 15:32:30 +00:00
```
2018-10-29 19:13:09 +00:00
The optional `format` argument allows you to specify and override which data type is contained
2019-03-16 09:01:11 +00:00
within the file specified in the `path` argument. Valid entries are `toml` , `json` , `csv`
2019-11-26 19:30:30 +00:00
or `plain` . If the `format` argument isn't specified, then the path extension is used.
2018-10-18 15:32:30 +00:00
```jinja2
2019-03-16 09:01:11 +00:00
{% set data = load_data(path="content/blog/story/data.txt", format="json") %}
2018-10-18 15:32:30 +00:00
```
2019-03-16 09:01:11 +00:00
Use the `plain` format for when your file has a toml/json/csv extension but you want to load it as plain text.
2019-11-26 19:30:30 +00:00
For *toml* and *json* , the data is loaded into a structure matching the original data file;
however, for *csv* there is no native notion of such a structure. Instead, the data is separated
2018-10-18 21:09:32 +00:00
into a data structure containing *headers* and *records* . See the example below to see
2018-10-18 15:32:30 +00:00
how this works.
In the template:
```jinja2
2019-03-16 09:01:11 +00:00
{% set data = load_data(path="content/blog/story/data.csv") %}
2018-10-18 15:32:30 +00:00
```
2019-03-16 09:01:11 +00:00
In the *content/blog/story/data.csv* file:
2018-10-29 19:13:09 +00:00
```csv
2018-10-18 15:32:30 +00:00
Number, Title
1,Gutenberg
2,Printing
```
The equivalent json value of the parsed data would be stored in the `data` variable in the
template:
```json
{
"headers": ["Number", "Title"],
"records": [
2018-10-18 21:09:32 +00:00
["1", "Gutenberg"],
2018-10-18 15:32:30 +00:00
["2", "Printing"]
],
}
2018-10-29 19:13:09 +00:00
```
#### Remote content
2019-11-26 19:30:30 +00:00
Instead of using a file, you can load data from a remote URL. This can be done by specifying a `url` parameter
to `load_data` rather than `path` .
2018-10-29 19:13:09 +00:00
```jinja2
{% set response = load_data(url="https://api.github.com/repos/getzola/zola") %}
{{ response }}
```
2019-11-26 19:30:30 +00:00
By default, the response body will be returned with no parsing. This can be changed by using the `format` argument
as below.
2018-10-29 19:13:09 +00:00
```jinja2
{% set response = load_data(url="https://api.github.com/repos/getzola/zola", format="json") %}
{{ response }}
```
2019-11-26 19:30:30 +00:00
#### Data caching
2018-10-29 19:13:09 +00:00
2019-11-26 19:30:30 +00:00
Data file loading and remote requests are cached in memory during the build, so multiple requests aren't made
to the same endpoint.
URLs are cached based on the URL, and data files are cached based on the file modified time.
The format is also taken into account when caching, so a request will be sent twice if it's loaded with two
different formats.
2018-10-18 15:32:30 +00:00
2018-01-16 12:57:31 +00:00
### `trans`
2019-02-27 13:54:09 +00:00
Gets the translation of the given `key` , for the `default_language` or the `lang` uage given
2018-01-16 12:57:31 +00:00
```jinja2
2018-05-17 16:32:46 +00:00
{{/* trans(key="title") */}}
{{/* trans(key="title", lang="fr") */}}
2018-01-16 12:57:31 +00:00
```
2018-04-08 20:12:39 +00:00
### `resize_image`
Resizes an image file.
2019-05-27 12:35:14 +00:00
Pease refer to [_Content / Image Processing_ ](@/documentation/content/image-processing/index.md ) for complete documentation.