liveBook · Manning

Template

As explained in The Well-Grounded Python Developer: How the pros use Python and Flask

A template is a document containing fixed content and placeholders or directives that are resolved to produce a rendered document.

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

How The Well-Grounded Python Developer: How the pros use Python and Flask covers it

How deep it goes
Works from placeholder substitution through inheritance, iteration, conditionals, and custom filters without detailing template-engine internals.
Examples
Uses a current-time homepage and a post list rendered as Bootstrap cards.
What it assumes
Assumes familiarity with Python functions and HTML while introducing templates through a Python f-string analogy.
What you can do after
Apply it via Flask and Jinja2 to render dynamic pages with shared layouts, post lists, and formatted timestamps.
How it connects
Links Flask routes, Jinja2, HTML, database queries, template inheritance, and session-based time-zone formatting.

Also explained in

Compare how 21 books explain template →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Well-Grounded Python Developer: How the pros use Python and Flask. The section links point to the parts of the book it draws on.