Jinja2 Templates — Rendering HTML Pages
In this chapter you will learn to use the Jinja2 template engine to render complete HTML pages and extract common layouts.
From Strings to Templates
In the previous chapter, view functions returned strings directly with HTML tags embedded inside, which could not be reused and was hard to maintain.
render_template() is the entry point for rendering templates in Flask: it loads an HTML file, injects variables, and returns a complete HTML response.
Example
File path: app.py
from
flask
import
Flask
,
render_template
app
=
Flask
(
name
)
@
app.
route
(
"/"
)
def
index
(
)
:
render_template('template_name', variable=value, ...)
return
render_template
(
'index.html'
,
title
=
'RUNOOB Blog'
)
Flask looks for templates in the templates/ folder in the project root by default.
Create Template Files
Create a templates/ folder in the project root, then create index.html。
Example
{{ title }}
{{ title }}
Welcome, this is a page rendered with the Jinja2 template engine.
Jinja2 Template Syntax Quick Reference
Jinja2 syntax is almost identical to Django template syntax and also similar to Vue3's interpolation syntax.
| Purpose | Jinja2 / Django | Description |
|---|---|---|
| Output variables | {{ variable }} |
Supports attribute access: {{ user.name }} |
| Loop | {% for item in list %}...{% endfor %} |
Supports loop.index |
| Conditional | {% if cond %}...{% endif %} |
Can be chained: {% elif %} |
| inheritance | {% extends 'base.html' %} |
Must be on the first line |
| Block definition | {% block name %}...{% endblock %} |
Child templates fill the placeholders in the parent template |
Jinja2 and Django template syntax are nearly identical, but Jinja2 is more flexible: you can write more complex Python expressions in templates, while Django templates deliberately restrict some capabilities to avoid business logic leaking into templates.
Template inheritance: base.html
When multiple pages share a navbar and footer, use template inheritance to avoid duplicating HTML.
Create the base.html parent template
Example
{% block title %}RUNOOB Blog{% endblock %}
{% block extra_head %}{% endblock %}
RUNOOB Blog (Flask)
{% block content %}
{% endblock %}
Child Template Inheriting the Parent Template
Example
{% extends 'base.html' %}
{% block title %}RUNOOB Blog - Home{% endblock %}
{% block content %}
Latest Articles
The article list will be shown in later chapters.
{% endblock %}
{% extends 'base.html' %}It must be written on the first line of the child template (except comments). This is a hard requirement of Jinja2—the template engine needs to determine the inheritance relationship first.
url_for() for Generating Links
Do not hardcode URLs; use url_for() to look up addresses by function name.
Example
Define route endpoints in views.py
@
app.
route
(
"/"
)
def
index
(
)
: ...
@
app.
route
(
"/post/int:post_id"
)
def
post_detail
(
post_id
)
: ...
Example
[mycode4 type="html">
Home
Article 3
[/mycode4]
The route changes (e.g., /post/ changed to /article/), the url_for() in the template automatically generates the new address.
Static File Configuration
By default, Flask uses the static/ directory as the static file root.
blog_project/
├── static/
│ ├── css/
│ │ └── style.css
│ ├── js/
│ │ └── main.js
│ └── images/
│ └── logo.png
├── templates/
└── app.py
In templates, reference them via url_for('static', filename='css/style.css') to reference it.
Chapter Summary
In this chapter you mastered the core of Jinja2 templates: render_template() for rendering templates, {{ }} for outputting variables, {% for/if %} for control flow, base.html + block + extends for template inheritance, and url_for() for generating links.
Now the blog pages are well-structured HTML, with common parts extracted into base.html.