Templates, Jinja, Dynamic HTML Rendering, and Layout Reuse
Render server-side HTML using Jinja templates and build reusable layouts cleanly in Flask.
Inside this chapter
- Why Templates Matter
- Basic Template Rendering
- Template Features
- Base Layouts
- Business Example
Series navigation
Study the chapters in order for the clearest path from Flask basics to scalable application design, APIs, security, and production operations. Use the navigation at the bottom to move smoothly through the full tutorial series.
Why Templates Matter
Flask is not only for APIs. It can also generate dynamic HTML pages for dashboards, admin tools, user portals, and content-driven applications. Templates let Python data be combined with HTML presentation safely and clearly.
Basic Template Rendering
from flask import render_template
@app.route("/welcome")
def welcome():
return render_template("welcome.html", name="Asha")
<h1>Welcome, {{ name }}</h1> Template Features
- Variable interpolation with
{{ }} - Conditionals and loops
- Template inheritance for shared layouts
- Filters for formatting values
Base Layouts
Template inheritance allows shared headers, navigation, and page wrappers to live in one place. This is important for maintainability in multi-page applications.
Business Example
A reporting portal may reuse one layout across login, dashboards, analytics tables, and settings pages. Template inheritance avoids copying the same header, footer, and navigation markup everywhere.