Templates with Jinja2
Render dynamic HTML.
Templates with Jinja2 is a free Python Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Python Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Templates?
Returning HTML from Python strings gets messy fast. Templates keep your HTML in separate files and fill in data at render time. Flask uses the Jinja2 engine.
from flask import Flask, render_template
app = Flask(__name__)
# render_template loads a file from the templates/ folder
print('Jinja2 powers Flask templates')The templates Folder
Flask looks for templates in a folder named templates/ next to your app. Files usually end in .html.
# project/
# app.py
# templates/
# index.html
print('Templates live in templates/')render_template
render_template('index.html') loads a template and returns the rendered HTML as the response.
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html')
print('home renders index.html')Passing Data
Pass variables as keyword arguments. The template can then display them.
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def home():
return render_template('index.html', name='Alice')
print('name=Alice is sent to the template')Outputting Variables
In the template, {{ name }} prints a variable. The double braces are Jinja2's expression syntax.
# index.html
# <h1>Hello, {{ name }}!</h1>
print('Double braces output a value')If Statements
Jinja2 control statements use {% %}. An if block conditionally shows content.
# {% if user %}
# <p>Welcome back!</p>
# {% else %}
# <p>Please log in.</p>
# {% endif %}
print('{% if %} branches the markup')For Loops
Loop over a list with {% for item in items %} to render repeated markup, such as a list of products.
# <ul>
# {% for item in items %}
# <li>{{ item }}</li>
# {% endfor %}
# </ul>
print('{% for %} repeats markup per item')Filters
Filters transform a value with a pipe: {{ name|upper }} uppercases it. Built-in filters include length, default, and title.
# {{ name|upper }} -> ALICE
# {{ items|length }} -> count
# {{ bio|default('N/A') }}
print('Filters transform values with |')Template Inheritance
A base template defines the page shell with {% block %} placeholders. Child templates {% extends %} it and fill the blocks.
# base.html
# <body>{% block content %}{% endblock %}</body>
#
# page.html
# {% extends 'base.html' %}
# {% block content %}<p>Hi</p>{% endblock %}
print('extends + block share a layout')Auto-Escaping
Jinja2 automatically escapes HTML in variables, protecting against cross-site scripting. A value like <script> is shown as text, not executed.
# If name = '<b>x</b>', {{ name }} renders the literal text
# Auto-escaping is on by default for safety
print('Auto-escaping prevents XSS')Linking with url_for
Inside templates you can still call url_for, often for static files: {{ url_for('static', filename='style.css') }}.
# <link rel='stylesheet'
# href="{{ url_for('static', filename='style.css') }}">
print('url_for works inside templates too')Quick Check
Test your Jinja2 knowledge.
Recap
You learned Jinja2 templating.
render_template()renders files fromtemplates/{{ }}outputs values;{% %}runsif/for- Filters like
|uppertransform values extends+blockenable layout inheritance- Auto-escaping guards against XSS
Frequently asked questions
Is the “Templates with Jinja2” lesson free?
Yes — the full text of “Templates with Jinja2” is free to read here on the web, and the Python Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Python Academy course, upgrade to CoddyKit PRO.
What will I learn in “Templates with Jinja2”?
Render dynamic HTML. You practise Python Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Python Academy?
No prior experience is required. Python Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Templates with Jinja2” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Python Academy lesson?
Yes. Every Python Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Routes and Views
- Templates with Jinja2
- Forms and Request Data
- Building a REST Endpoint