A web page often needs both stable HTML and values that change with each request. A template keeps the HTML in a separate file and marks where dynamic values belong. Think of `f"My name is {name}"`: it combines fixed text with a supplied value. With Flask, Jinja2 renders the completed HTML before the server sends it to the browser. section 6.4.3
from flask import Flask, render_template #1
from datetime import datetime #2
app = Flask(__name__)
@app.route("/")
def home():
return render_template("index.html", now=datetime.now()) #3
A Flask route calls `render_template("index.html", now=datetime.now())`. The `index.html` template can place that value in a heading with `{{now}}`. section 6.4.3
As an application grows, templates can do more than insert a single value:
- Inherit a shared `base.html` layout, so page-specific templates supply their own content without repeating common elements. section 11.4.2
- Iterate over posts supplied by a handler and render a short excerpt of each as a Bootstrap card. The database query, rather than the template, determines their newest-first order. section 11.4.2
- Conditionally show page elements, such as an Active badge or Update button, and pass values through filters before display. section 11.6.2
<li class="list-group-item">
Created: {{ post.created | format_datetime | safe }}
</li>
<li class="list-group-item">
Updated: {{ post.updated | format_datetime | safe }}
</li>
Here, the post template pipes stored timestamps through `format_datetime` and `safe`. The custom `format_datetime` filter uses time-zone information from the user's session to display local times. section 11.6.2
Keep the responsibilities distinct: a handler gathers data, and a template renders it as an HTML page. That pattern works for a changing clock, a list of database posts, or a page where someone enters and reviews a post. section 6.4.3 section 11.5




















