In a typical Web program, accessing an address usually returns an HTML page containing various types of information. Because our programs are dynamic, some of the information on the page needs to be adapted to different situations, such as displaying different information for logged in and unlogged in users, so the page needs to be dynamically generated according to the program logic when the user accesses it.
We call text in HTML or other formats containing variables and arithmetic logic templates, and the process of performing these variable substitutions and logic calculations is called rendering (template rendering engine - Jinja2).
By default, Flask looks for templates in the templates folder in the same directory as the module where the program instance resides.
Basic syntax of templates
In the template, you need to add specific delimiters to mark up Jinja2 statements and variables
Here are three commonly used delimiters:
1. {{ ... }} is used to mark variables.
2, {% ... %} used to mark statements, such as if statements, for statements, etc..
3. {# ... #} Used to write comments.
Variables used in templates need to be passed in at render time.
Writing homepage templates
Let's create a file in the templates directory as a template for the homepage. The homepage needs to display a list of movie entries and personal information, as shown in the code below:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>movie list</title> </head> <body> {# Use the length filter to get the length of the movies variable #} <p>{{ movies|length }} Titles</p> <ul> {% for movie in movies %} {# Iterate over movies variables #} <li>{{ }} - {{ }}</li> {# Equivalent to movie['title'] #} {% endfor %} {# Use the endfor tag to end a for statement #} </ul> </body> </html>
In order to facilitate the handling of variables, Jinja2 provides a number of filters (which are similar to those in the framework) with the following syntax:
{{ variable|filter }}
The left-hand side is the variable, and the right-hand side is the name of the filter. For example, the template above uses the length filter to get the length of the movies, similar to the len() function in Python.
Preparing virtual data
In order to simulate the page rendering, we need to first create some dummy data that will be used to populate the page content, in which the dummy data is defined.
movies = [{'title': 'Lake Nagatsu', 'year': '2021'},{'title': 'Send you a little red flower', 'year': '2021'}]
Render homepage template
The template can be rendered out using the render_template() function, which must be passed the template filename (as opposed to the path to the file in the templates root directory), in this case ''. In order for the template to be rendered correctly, we also need to pass the variables used inside the template into this function via keyword arguments, as shown below:
from flask import Flask, render_template app = Flask(__name__) @('/') def movie(): return render_template('', movies=movies) if __name__ == "__main__": ()
incomingrender_template()
In the function's keyword argument, the movies on the left is the name of the variable used in the template, and the movies on the right is the actual object that the variable points to. The movies passed into the template are lists, but that's not the only Python data structure that can be used in templates; you can also pass in strings, tuples, dictionaries, functions, and so on.
render_template()
When called, the function recognizes and executes all Jinja2 statements in "", returning the rendered template content. On the returned page, variables are replaced with their actual values (including delimiters), and statements (and delimiters) are removed after execution (comments are also removed).
summarize
That's all for this post, I hope it helped you and I hope you'll check back for more from me!