Jinja Template Variables
To declare the variable using Jinja Template we use {{variable_name}} within the HTML file. As a result, the variable will be displayed on the Website.
Syntax of Jinja Template Variables
{{any_variable_name}}
variable_example.html
HTML
< html > < head > < title >Variable Example</ title > </ head > < body > < h3 >Hello {{name}}</ h3 > </ 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