Jinja Template for Loop
Jinja for loop syntax is similar to the if statements, the only difference is for loop requires sequence to loop through.
Syntax of Jinja Template for Loops
{% for variable_name in sequence%} ... ... ... {% endfor %}
for_example.html
HTML
<!DOCTYPE html> < html > < head > < title >For Example</ title > </ head > < body > < h2 > w3wiki Available Course </ h2 > {% for course in courses%} < h4 > {{course}} </ h4 > {% endfor %} </ body > </ html > |
Output:
Templating With Jinja2 in Flask
Flask is a lightweight WSGI framework that is built on Python programming. WSGI simply means Web Server Gateway Interface. Flask is widely used as a backend to develop a fully-fledged Website. And to make a sure website, templating is very important. Flask is supported by inbuilt template support named Jinja2. Jinja2 is one of the most used Web template engines for Python. This Web template engine is a fast, expressive, extensible templating engine. Jinja2 extensively helps to write Python code within the HTML file. Further, it also includes:
- Async support for generating templates that automatically handle sync and async functions without extra syntax.
- Template inheritance and inclusion.
- The Template engine makes debugging easier.
- Support of both High-level and Low-level API support.
Install the required package
To install the Jinja2 package in Python, check your latest pip version and stay updated. Install Jinja2 using the following command:
pip install Jinja2
But since we are dealing with the Templating with Jinja2 in Flask, there is no need to separately install Jinja2. When you install the Flask framework, the Jinja2 comes installed with it.
pip install flask
Contact Us