Synopsis
A Jinja template is simply a text file. Jinja can generate any text-basedformat (HTML, XML, CSV, LaTeX, etc.). A Jinja template doesn’t need to have aspecific extension: .html
, .xml
, or any other extension is just fine.
A template contains variables and/or expressions, which get replacedwith values when a template is rendered; and tags, which control thelogic of the template. The template syntax is heavily inspired by Django andPython.
Below is a minimal template that illustrates a few basics using the defaultJinja configuration. We will cover the details later in this document:
<!DOCTYPE html>
<html lang="en">
<head>
<title>My Webpage</title>
</head>
<body>
<ul id="navigation">
{% for item in navigation %}
<li><a href="{{ item.href }}">{{ item.caption }}</a></li>
{% endfor %}
</ul>
<h1>My Webpage</h1>
{{ a_variable }}
{# a comment #}
</body>
</html>
The following example shows the default configuration settings. An applicationdeveloper can change the syntax configuration from {% foo %}
to <% foo%>
, or something similar.
There are a few kinds of delimiters. The default Jinja delimiters areconfigured as follows:
{% … %}
for Statements{{ … }}
for Expressions to print to the template output{# … #}
for Comments not included in the template output# … ##
for Line Statements