python-backend
/

Flask Templates – Dynamic HTML with Jinja2

Last Sync: Today

On this page

5
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

python-backend

Flask Templates – Dynamic HTML with Jinja2

What are Flask Templates?

While APIs return JSON, web applications often need to serve HTML. Flask uses the Jinja2 engine to generate dynamic HTML. This allows you to write standard HTML files with special placeholders and logic that Flask fills in before sending the page to the user's browser.

  1. Rendering a Template

By default, Flask looks for HTML files in a folder named templates/ at the root of your project. You use the render_template() function to serve these files and pass data to them.

PythonRead-only
1
from flask import Flask, render_template

app = Flask(__name__)

@app.route('/')
def home():
    user_name = "Kishore"
    skills = ["Flutter", "Python", "Clean Architecture"]
    return render_template('index.html', name=user_name, tech_stack=skills)

  1. Jinja2 Syntax Basics

Jinja2 uses specific delimiters to distinguish between plain HTML and Python-like logic.

  • {{ variable }}: Expressions used to print output to the page.
  • {% statement %}: Control structures like loops or conditionals.
  • {# comment #}: Comments that won't appear in the final HTML.
HTMLRead-only
1

<h1>Welcome, {{ name }}!</h1>

<h3>Your Skills:</h3>
<ul>
    {% for skill in tech_stack %}
        <li>{{ skill }}</li>
    {% else %}
        <li>No skills listed.</li>
    {% endfor %}
</ul>

  1. Template Inheritance

To avoid repeating headers, footers, and navigation bars on every page, Flask uses inheritance. You create a base 'layout' and other pages 'extend' it.

HTMLRead-only
1

<html>
  <body>
    <nav>Navigation Bar</nav>
    {% block content %}{% endblock %}
  </body>
</html>


{% extends 'base.html' %}
{% block content %}
  <h2>This is the Home Page</h2>
{% endblock %}

Common Jinja2 Features

FeatureSyntaxDescription
Filters{{ name|upper }}Modifies variables (e.g., uppercase, length)
Conditionals{% if user %}...{% endif %}Shows content based on logic
Includes{% include 'footer.html' %}Imports a small snippet of HTML
URL Building{{ url_for('static', filename='style.css') }}Generates paths to static files

Test Your Knowledge

Q1
of 3

Which Jinja2 delimiter is used for printing a variable?

A
{% ... %}
B
{{ ... }}
C
{# ... #}
D
<% ... %>
Q2
of 3

Where must template files be stored by default in a Flask project?

A
/html
B
/views
C
/templates
D
/pages
Q3
of 3

Which tag is used to allow a child template to insert content into a parent template?

A
{% include %}
B
{% extends %}
C
{% block %}
D
{% insert %}

Frequently Asked Questions

Can I use Python functions inside Jinja2?

Not directly. You can only use functions that you explicitly pass to render_template or those that Flask provides by default (like url_for).

What is the 'safe' filter?

By default, Jinja2 escapes HTML for security. If you want to render actual HTML tags stored in a variable, use {{ my_html|safe }}. Be careful with user input!

Where do I put my CSS and Images?

Put them in a folder named 'static/'. In your template, reference them using: {{ url_for('static', filename='css/main.css') }}.

Previous

flask routing

Next

flask request response

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.