
Welcome!
Everything is fine.
Here's the Python code in example_template_inheritance.py
:
# -*- coding: utf-8 -*-
from flask import (Flask, url_for, render_template, request, redirect, flash)
from all_examples import app
@app.route('/base/')
def base():
# the base template needs only one filler
return render_template('base.html',title='About')
# The reading has three inputs.
# Note the Unicode string; we'll talk about Unicode much later.
@app.route('/reading/')
def reading():
stuff = {'title' : '''Irish Welcomes (for class on St. Patrick's Day)''',
'classdate' : '3/17/2017',
'material' : u'céad míle fáilte or a hundred thousand welcomes'}
return render_template('reading.html',
title=stuff.title,
classdate=stuff.classdate,
material=stuff.material)
@app.route('/inclass/')
def inclass():
stuff = {'title' : 'Irish Welcomes',
'toc' : 'roll call, assignments, review, new material',
'content' : 'The Irish speak "Irish" not "Gaelic".'}
return render_template('inclass.html',
title=stuff.title,
classdate=stuff.classdate,
material=stuff.material)
There are, of course, three HTML templates. Here's the base.html
:
<!doctype html>
<html lang='en'>
<head>
<meta charset='utf-8'>
<meta name=author content="Scott D. Anderson">
<title>{{title}}</title>
<link rel="stylesheet"
href="{{url_for('static',filename='inheritance.css')}}"
type="text/css">
{% block headstuff %}{% endblock %}
</head>
<body>
<header id="top">
{% block top %}
This text is at the top of every page, unless overridden.
{% endblock %}
</header>
<article id="content">
{% block content %}
<h1>{{title}}</h1>
<p>This stuff appears on every page, unless overridden.</p>
<p>Lorem ipsum ...</p>
{% endblock %}
</article>
<footer>
{% block footer %}
Default Copyright notices and stuff
{% endblock %}
</footer>
</body>
</html>
And here's the first child, reading.html
:
{% extends "base.html" %}
{% block top %}
Overridden by the reading.html template
{% endblock %}
{# replaces default content block,
adds holes classdate and material #}
{% block content %}
<h1>Reading on {{title}}</h1>
<p>Please read the following before class on {{ classdate }}</p>
<section>{{ material }}</section>
{% endblock %}
{# replaces default footer #}
{% block footer %}
© 2019 Scott D. Anderson and the CS 304 staff
{% endblock %}
And here's the second child, inclass.html
:
{% extends "base.html" %}
{# additional stuff for the head #}
{% block headstuff %}
<style> .optional { color: gray; }</style>
{% endblock %}
{# replaces default content block #}
{% block content %}
<h1>Class Activities on {{title}}</h1>
<p class="optional">We'll do the following in class; no need to read beforehand.</p>
<nav>{{toc}}</nav>
<section>{{content}}</section>
{% endblock %}