You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

overview.md 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. +++
  2. title = "Overview"
  3. weight = 10
  4. +++
  5. Zola uses the [Tera](https://tera.netlify.com) template engine and is very similar
  6. to Jinja2, Liquid or Twig.
  7. As this documentation will only talk about how templates work in Zola, please read
  8. the [Tera template documentation](https://tera.netlify.com/docs/templates/) if you want
  9. to learn more about it first.
  10. All templates live in the `templates` directory. If you are not sure what variables are available in a template, you can just stick `{{ __tera_context }}` in it
  11. to print the whole context.
  12. A few variables are available on all templates minus RSS and sitemap:
  13. - `config`: the [configuration](@/documentation/getting-started/configuration.md) without any modifications
  14. - `current_path`: the path (full URL without the `base_url`) of the current page, never starting with a `/`
  15. - `current_url`: the full URL for that page
  16. - `lang`: the language for that page, `null` if the page/section doesn't have a language set
  17. The 404 template does not get `current_path` and `current_url` as it cannot know it.
  18. ## Standard Templates
  19. By default, Zola will look for three templates: `index.html`, which is applied
  20. to the site homepage; `section.html`, which is applied to all sections (any HTML
  21. page generated by creating a directory within your `content` directory); and
  22. `page.html`, which is applied to all pages (any HTML page generated by creating a
  23. `.md` file within your `content` directory).
  24. The homepage is always a section (regardless of whether it contains other pages).
  25. Thus, the `index.html` and `section.html` templates both have access to the
  26. section variables. The `page.html` template has access to the page variables.
  27. The page and section variables are described in more detail in the next section of this documentation.
  28. ## Built-in Templates
  29. Zola comes with three built-in templates: `rss.xml`, `sitemap.xml`, and
  30. `robots.txt` (each described in their own section of this documentation).
  31. Additionally, themes can add their own templates, which will be applied if not
  32. overridden. You can override built-in or theme templates by creating a template with
  33. same name in the correct path. For example, you can override the RSS template by
  34. creating a `templates/rss.xml` file.
  35. ## Custom Templates
  36. In addition to the standard `index.html`, `section.html`, and `page.html` templates,
  37. you may also create custom templates by creating a `.html` file in the `templates`
  38. directory. These custom templates will not be used by default. Instead, the 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:
  39. ```md
  40. +++
  41. title = "About Us"
  42. template = "about.html"
  43. +++
  44. ```
  45. Custom templates are not required to live at the root of your `templates` directory.
  46. For example, `product_pages/with_pictures.html` is a valid template.
  47. ## Built-in filters
  48. Zola adds a few filters, in addition of the ones [ones already present](https://tera.netlify.com/docs/templates/#built-in-filters) in Tera.
  49. ### markdown
  50. Converts the given variable to HTML using Markdown. This doesn't apply any of the
  51. features that Zola adds to Markdown: internal links, shortcodes etc won't work.
  52. By default, the filter will wrap all text into a paragraph. To disable that, you can
  53. pass `true` to the inline argument:
  54. ```jinja2
  55. {{ some_text | markdown(inline=true) }}
  56. ```
  57. ### base64_encode
  58. Encode the variable to base64.
  59. ### base64_decode
  60. Decode the variable from base64.
  61. ## Built-in global functions
  62. Zola adds a few global functions to [those in Tera](https://tera.netlify.com/docs/templates/#built-in-functions) in order to make it easier to develop complex sites.
  63. ### `get_page`
  64. Takes a path to a `.md` file and returns the associated page
  65. ```jinja2
  66. {% set page = get_page(path="blog/page2.md") %}
  67. ```
  68. ### `get_section`
  69. Takes a path to a `_index.md` file and returns the associated section
  70. ```jinja2
  71. {% set section = get_section(path="blog/_index.md") %}
  72. ```
  73. If you only need the metadata of the section, you can pass `metadata_only=true` to the function:
  74. ```jinja2
  75. {% set section = get_section(path="blog/_index.md", metadata_only=true) %}
  76. ```
  77. ### ` get_url`
  78. Gets the permalink for the given path.
  79. If the path starts with `@/`, it will be understood as an internal
  80. link like the ones used in markdown, starting from the root `content` directory.
  81. ```jinja2
  82. {% set url = get_url(path="@/blog/_index.md") %}
  83. ```
  84. This can also be used to get the permalinks for static assets for example if
  85. we want to link to the file that is located at `static/css/app.css`:
  86. ```jinja2
  87. {{/* get_url(path="css/app.css") */}}
  88. ```
  89. By default, assets will not have a trailing slash. You can force one by passing `trailing_slash=true` to the `get_url` function.
  90. An example is:
  91. ```jinja2
  92. {{/* get_url(path="css/app.css", trailing_slash=true) */}}
  93. ```
  94. In the case of non-internal links, you can also add a cachebust of the format `?t=1290192` at the end of a URL
  95. by passing `cachebust=true` to the `get_url` function.
  96. ### `get_taxonomy_url`
  97. Gets the permalink for the taxonomy item found.
  98. ```jinja2
  99. {% set url = get_taxonomy_url(kind="categories", name=page.taxonomies.category) %}
  100. ```
  101. The `name` will almost come from a variable but in case you want to do it manually,
  102. the value should be the same as the one in the front-matter, not the slugified version.
  103. ### `get_taxonomy`
  104. Gets the whole taxonomy of a specific kind.
  105. ```jinja2
  106. {% set categories = get_taxonomy(kind="categories") %}
  107. ```
  108. ### `load_data`
  109. Loads data from a file or URL. Supported file types include *toml*, *json* and *csv*.
  110. Any other file type will be loaded as plain text.
  111. The `path` argument specifies the path to the data file relative to your base directory, where your `config.toml` is.
  112. As a security precaution, If this file is outside of the main site directory, your site will fail to build.
  113. ```jinja2
  114. {% set data = load_data(path="content/blog/story/data.toml") %}
  115. ```
  116. The optional `format` argument allows you to specify and override which data type is contained
  117. within the file specified in the `path` argument. Valid entries are `toml`, `json`, `csv`
  118. or `plain`. If the `format` argument isn't specified, then the paths extension is used.
  119. ```jinja2
  120. {% set data = load_data(path="content/blog/story/data.txt", format="json") %}
  121. ```
  122. Use the `plain` format for when your file has a toml/json/csv extension but you want to load it as plain text.
  123. For *toml* and *json* the data is loaded into a structure matching the original data file,
  124. however for *csv* there is no native notion of such a structure. Instead the data is separated
  125. into a data structure containing *headers* and *records*. See the example below to see
  126. how this works.
  127. In the template:
  128. ```jinja2
  129. {% set data = load_data(path="content/blog/story/data.csv") %}
  130. ```
  131. In the *content/blog/story/data.csv* file:
  132. ```csv
  133. Number, Title
  134. 1,Gutenberg
  135. 2,Printing
  136. ```
  137. The equivalent json value of the parsed data would be stored in the `data` variable in the
  138. template:
  139. ```json
  140. {
  141. "headers": ["Number", "Title"],
  142. "records": [
  143. ["1", "Gutenberg"],
  144. ["2", "Printing"]
  145. ],
  146. }
  147. ```
  148. #### Remote content
  149. 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`.
  150. ```jinja2
  151. {% set response = load_data(url="https://api.github.com/repos/getzola/zola") %}
  152. {{ response }}
  153. ```
  154. By default, the response body will be returned with no parsing. This can be changed by using the `format` argument as below.
  155. ```jinja2
  156. {% set response = load_data(url="https://api.github.com/repos/getzola/zola", format="json") %}
  157. {{ response }}
  158. ```
  159. #### Data Caching
  160. Data file loading and remote requests are cached in memory during build, so multiple requests aren't made to the same endpoint.
  161. URLs are cached based on the URL, and data files are cached based on the files modified time.
  162. The format is also taken into account when caching, so a request will be sent twice if it's loaded with 2 different formats.
  163. ### `trans`
  164. Gets the translation of the given `key`, for the `default_language` or the `lang`uage given
  165. ```jinja2
  166. {{/* trans(key="title") */}}
  167. {{/* trans(key="title", lang="fr") */}}
  168. ```
  169. ### `resize_image`
  170. Resizes an image file.
  171. Pease refer to [_Content / Image Processing_](@/documentation/content/image-processing/index.md) for complete documentation.