What is Jinja ?
13 March, 2023
2
2
0
Contributors
Jinja is a popular templating engine for Python that is widely used in web development. It is especially popular in the Flask web framework, where it is the default templating engine.
Jinja makes it easy to generate dynamic HTML pages by allowing developers to embed Python code directly within HTML templates. This makes it possible to generate pages that are customized for each individual user, or that display different content depending on various conditions.
Key features of Jinja2 :
- Jinja supports template inheritance, allowing developers to create a base template that can be extended with additional content as needed.
- Jinja provides a variety of filters that can be used to transform template variables in various ways, such as converting strings to uppercase or formatting dates.
- For example, a developer might use the "upper" filter to convert a string to all uppercase letters, or the "date" filter to format a date in a particular way.
So, how to implement Jinja in our application ?
Rendering a template
By default Flask looks for templates in a templates subfolder located inside the application folder. For the next version of hello.py, you need to store the templates defined in a new ' templates ' folder as index.html and user.html.
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/user/<name>')
def user(name):
return render_template('user.html', name=name) #we are passing the variable name as name
if __name__ == '__main__':
app.run(debug=True)
In the above code the function render_template
integrates the Jinja2 template engine with the application.
The first argument taken is the name of the template while second one, is for variables.
Variables in Jinja2
In a template the constructs {{ name }} tells the template engines that a variable name
is put here. You can even use lists and dictionary data types.
<p>A value from a dictionary: {{ flaskdict['key'] }}.</p> #using a dictionary
<p>A value from a list: {{ flasklist[3] }}.</p> #using a list
We can use filters with variables using the following syntax {{ variable | filter }}
| Filter name | Description
| safe | Renders the value without applying escaping
| capitalize | Converts the first character of the value to uppercase and the rest to lowercase
| lower | Converts the value to lowercase characters
| upper | Converts the value to uppercase characters
| trim | Removes leading and trailing whitespace from the value
| striptags | Removes any HTML tags from the value before rendering
| abs | Return the absolute value of the argument.
Control Structure
In flask every control structure starts with {% x %} and ends with {{% endif %}}
- For Loop
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
Some special variables that can be used inside for-block
.
With below code you can cycle among a list of strings/variables each time through the loop by using the special loop.cycle
helper:
{% for row in rows %}
<li class="{{ loop.cycle('odd', 'even') }}">{{ row }}</li>
{% endfor %}
- If Loop
{% if users %}
{% for user in users %}
<li>{{ user.username|e }}</li>
{% endfor %}
{% endif %}
- Macros
You can think of Macros as functions in Flask.
{% macro render_comment(comment) %}
<li>{{ comment }}</li>
{% endmacro %}