Hello there! Today, we will take a look at the Jinja templating language again. But this time we will talk about conditions.
We think you already know what conditions are: if, else, and other cool things. If you tried to write your applications, you probably needed to generate page elements depending on the external information. How cool would it be to create a language to help us maintain dynamic web pages? Jinja is exactly what you need to embed conditions and loops (more on that later) into our HTML templates. Let's see what it looks like:
<!DOCTYPE html>
<html>
<head>
<title>Mega Template</title>
</head>
<body>
<h1>There is a text</h1>
{% if flag == True %}
<h2>And this text is displayed only when the argument, that passed into the render_template method equals True</h2>
{% endif %}
</body>
</html>
This is an example of a simple template with conditions. As you can see, the syntax for conditions in Jinja templates employs the {% %} symbols. Also, the structure of what's inside is very similar to the default Python conditions (loops are similar, too). In general, we can imagine that code snippets in {% %} contain regular Python code. Let's create something!
Filling data
First, we will create a simple application:
from flask import Flask, render_template
app = Flask('main')
@app.route('/')
def main_view():
return render_template('main.html', flag=True)
app.run()
Now, let's try to reach our page:
Perfect! Our condition worked and we can see the text. But what if we change the flag value? Let's swap it to flag=False when returning a template. You will see this:
Fantastic! This example can prove that our condition is truly working and we can use them in the future. By the way, as the syntax is pretty similar to python, we can also state the condition in the following way:
{% if flag %}
The conditions in Jinja templates can be used for any purpose the same way as in Python — it can not only check the truthiness of a variable but also compare values.
Advanced usage
Another great thing about Jinja conditions is that we can use Python default data types, such as lists, dictionaries, and others while defining conditional statements. Yes, Jinja support lists from Python! Let's take a quick look:
<!DOCTYPE html>
<html>
<head>
<title>Mega Template</title>
</head>
<body>
<h1>There is a eternal text</h1>
{% if flags[0] %}
<h2>And this text is displayed only when the first argument, that passed into the render_template as list method equals True</h2>
{% elif flags[1] or flags[2] %}
<h2>Oh! This text is displayed when we define the second element of the passed list as True!</h2>
{% else %}
<h2>Gorgeus! Third argument, that displayed only when third element of passed list is True!</h2>
{% endif %}
</body>
</html>
In the template above, we pass an argument of the list datatype with boolean values to provide proper information. This is a very convenient way to control the contents of the page and change it depending on calculated or received data in Python. This way of using iterables is pretty convenient, so we can use dictionaries instead of lists. There's one thing that we need to change is only a condition that matches the Python conditions.
Note that we use the if-elif-else construction in the code snippet. We also calculate the if statement result using the or logical operator.
Conclusion
Today we learned about a very useful thing -- conditions in Jinja templates. The syntax is pretty much the same as in regular python, but we must always remember to use the magical brackets to make the code executable.