Using Flask Templates
Writing HTML in Python code is not a good practice. It is hard to maintain, understand and becomes really difficult with larger pages. Flask provides a way to write HTML in separate files and then use them in the Python code. These files are called templates which are built using Jinja2.
Jinja2 Templates
Templates are special HTML files that contain placeholders for dynamic content. These placeholders are replaced with actual values when the template is rendered. Templates are used to separate the presentation logic from the business logic.
Flask stores all of its templates in a folder called templates
. This folder should be created in the same directory as the app.py
file.
- app.py
Directorytemplates/
- index.html
Creating a Template
-
The template is based on an HTML file so requires the standard HTML structure.
-
Each part that would contain content is instead replaced by a placeholder enclosed in double curly braces
{{ }}
. These placeholders are replaced with actual values when the template is rendered.
Rendering a Template
To render a template, we need to use the render_template
function from the flask
module. This function takes the name of the template file and the values to be replaced in the placeholders.
When we run the server and visit the home page, we will see the content from the template file.
Complex Templates
Templates can be as complex as you want with lots of different functionality.
Base Template
Base templates can be used so you don’t have to repeat the same code in every template. Create a new file called base.html
in the templates
folder.
-
The
base.html
file contains the basic structure of an HTML file. -
The
title
andcontent
sections are enclosed in{% block %}
tags. These are placeholders that can be overridden in the child templates.Instead of putting content from a variable inside of these tags they will instead be filled with a block of content from the child template. Instead of using
{{ variable_name }}
we use{% block block_name %}{% endblock %}
which indicates it is a block instead of a variable. -
As these are blocks, they can be overridden in the child templates by using the same block name.
They are overridden by using the
{% block %}
tag with the same name as the block in the parent template.
We will then need to update our index.html
file to extend the base.html
file.
This would give a index.html
that now looks like this:
-
Using
{% extends 'base.html' %}
we indicate that theindex.html
file extends thebase.html
file. This means that the content of theindex.html
file will be inserted into thebase.html
file. -
We then use
{% block title %}Home{% endblock %}
to override thetitle
block in thebase.html
file. -
We then use
{% block content %}...{% endblock %}
to override thecontent
block in thebase.html
file.
When we run the server and visit the home page, we will see the content from the template file.
Includes
Includes can be used to include a file in another file. Create a new file called header.html
in the templates
folder.
We will then need to update our base.html
file to include the header.html
file.
When we run the server and visit the home page, we will see the content from the template file.
Passing Variables to Templates
If you want to pass variables to the included file, you can do so by passing the variables as arguments to the include
function. We can modify our header.html
file to accept a title
variable.
We will then need to update our base.html
file to include the header.html
file with the title
variable.
When we run the server and visit the home page, we will see the content from the template file.
Loops
If we have a list of items that we want to display, we can use a loop to iterate over the list and display each item. Create a new file called list.html
in the templates
folder.
We will then need to update our app.py
file to render the list.html
file.
When we run the server and visit the list page, we will see the content from the template file.
Conditionals
If we want to display content based on a condition, we can use conditionals. Create a new file called conditional.html
in the templates
folder.
We will then need to update our app.py
file to render the conditional.html
file.
When we run the server and visit the conditional page, we will see the content from the template file.