Same shirt, every player
Every player in a team wears the same shirt. Only the number on the back changes.
Web pages in one app should be the same. The header, the menu and the footer stay put. Only the middle changes.
If you copy the header into twenty pages, one small change means twenty edits. There is a better way.
Key words
| Word | What it means | Example |
|---|---|---|
| Template | A page with gaps that the app fills in. | home.html |
| Jinja | The template language Flask uses. | |
| Base template | The one layout every other page extends. | |
| Block | A named gap in a template that a page fills. | |
| Skip link | A hidden link that jumps a keyboard user to the main content. | |
| Accessibility | Making an app work for everyone, including disabled people. |
The cells students fill in are left blank in this preview.
By the end of this lesson
Note
I can build a base template that every page of the app extends.
Starter: what you already know
Have a go
Starter: what you already know
3 questionsSprint 0, step 2: the goal
| Part | For this step |
|---|---|
| Sprint (a short block of work) | Sprint 0: a project that runs, a page layout and sample data. |
| This step | A base template, a home page and one stylesheet. |
| Story it starts | E6.3: as a keyboard user, I want to reach every link and button, so that I can use the app without a mouse. |
The cells students fill in are left blank in this preview.
A template is a page with gaps
Flask keeps HTML in templates, in a folder called templates. The language inside them is Jinja.
{{ ... }} prints a value. {% ... %} is a command, such as for or if.
The base template holds the parts every page shares. It leaves named gaps, called blocks. Each page says {% extends "base.html" %} and fills the blocks. Change the menu once and every page changes.
Step 2: the base template
{# helpdesk/templates/base.html (part) #}
<html lang="en-GB">
<head>
<title>{% block title %}Home{% endblock %} | Netheret help desk</title>
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
{# The skip link is the first thing a keyboard user reaches. #}
<a class="skip-link" href="#main">Skip to main content</a>
<header class="site-header"> ... the brand and the menu ... </header>
<main id="main" class="wrap" tabindex="-1">
{% block content %}{% endblock %}
</main>
<footer class="site-footer"> ... </footer>
</body>
</html>
Check: templates and blocks
Have a go
Check: templates and blocks
3 questionsStep 2: a page that extends it
{# helpdesk/templates/home.html (part) #}
{% extends "base.html" %}
{% block title %}Home{% endblock %}
{% block content %}
<h1>Netheret help desk</h1>
<p class="lead">Log support requests, plan on-site jobs and see how the help desk is doing.</p>
{% endblock %}
The route now returns a template
In step 1 the home page returned some HTML as text. Now it calls render_template("home.html"). Flask finds the file, fills the blocks and sends the page.
Python decides what to show. The template decides how it looks. Keeping the two apart makes the code easier to read and to change.
Step 2: home() uses render_template
# helpdesk/main.py, step 2
from flask import Blueprint, render_template
bp = Blueprint("main", __name__)
@bp.route("/")
def home():
# render_template finds home.html in the templates folder.
return render_template("home.html")
Make a free account to save your work and track your progress.
Match each part to its job
Have a go
Loading…
Your answers are marked here and are not kept. A college account keeps every student’s marks.
A common mistake
Warning
Many students copy the header and menu into every page. It works at first. Then one menu change means editing every page, and some get missed. Put shared parts in the base template once. A page should only hold what is different about it.
Accessible from the very first page
Some people use only a keyboard. They press Tab to move from link to link.
The skip link is the first thing Tab reaches. It jumps past the menu to the main content. It stays hidden until it gets focus.
A yellow focus ring shows where you are. lang="en-GB" tells a screen reader which voice to use. All the colours are CSS variables (named values set once), so they change in one place.
Try it: press Tab
<a class="skip-link" href="#main">Skip to main content</a>
<header class="site-header">
<strong>Netheret</strong> help desk
<nav><a href="#" aria-current="page">Home</a> <a href="#">Sign in</a></nav>
</header>
<main id="main" tabindex="-1">
<h1>Netheret help desk</h1>
<p>Click in this page, then press Tab. Watch the top left corner.</p>
<p><a href="#">A link to try the focus ring on</a></p>
</main>
Make a free account to save your work and track your progress.
Check: the accessible layout
Have a go
Check: the accessible layout
3 questionsBuild it yourself: step 2
Build step 2. 1. Make helpdesk/templates/base.html with a skip link, a header with a menu, a main area with a content block, and a footer. 2. Make home.html that extends it and fills the title and content blocks. 3. Make helpdesk/static/css/style.css with your colours as variables, a hidden skip link and a clear focus ring. 4. Change home() to use render_template. 5. Test it: press Tab on the page and check the skip link and the focus ring appear. 6. Commit and write the change log row.
A teacher marks this in a college. Nothing you write here is kept: it goes when you leave the page.
Make a free account to save your work, or press Start lab to work through the whole lesson.
Compare with the step 2 code
Tip
Compare yours with the step 2 code on GitHub. Look at style.css too: every colour is a variable at the top.
Change log entry for step 2
| Date | Version and files | What changed | Why |
|---|---|---|---|
| 3 March | step-02: base.html, home.html, style.css, main.py | A base template with header, menu, main and footer, a skip link and a focus ring | Every later page gets the same accessible layout without repeating it |
The cells students fill in are left blank in this preview.
Next: the database
The pages have nowhere to get data from yet. In step 3 you turn the Task 1 data design into Python classes, one for each table. A script fills the tables with sample data, so every later page has something real to show.
Exit quiz
Have a go