We briefly look at ways of taking the donkey work out of writing HTML:
templates and static site generation.
And we briefly look at CSS frameworks, which offer us pre-written CSS.
templates and static-site generation
Writing HTML for a single web page is nearly painless.
But writing HTML for a web site gets very repetitive:
most pages have the same design, but differ in some of their content.
Much later, we will look at dynamic web pages
(using Flask).
But even now, we can save work by using
static site generation:
creating HTML files from templates.
We'll investigate this using Jinja2 templates — because they're the same ones we'll use in Flask later.
staticJinja is a program that converts templates into regular HTML web pages.
Each page has the same header
and footer
; only their main
differs.
Let's say this is stored in _base.html
:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<header>
<nav>
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="about.html">About</a></li>
<li><a href="contact.html">Contact</a></li>
</ul>
</nav>
</header>
{% block main %}{% endblock %}
<footer>
<p>
<small>
© Derek Bridge
</small>
</p>
</footer>
</body>
</html>
{% extends '_base.html' %}
{% block title %}
Home
{% endblock %}
{% block main %}
<main>
<h1>Welcome!</h1>
<p>
This is the home
page.
</p>
</main>
{% endblock %}
{% extends '_base.html' %}
{% block title %}
About
{% endblock %}
{% block main %}
<main>
<h1>About us!</h1>
<p>
This is about who
we are.
</p>
</main>
{% endblock %}
{% extends '_base.html' %}
{% block title %}
Contact
{% endblock %}
{% block main %}
<main>
<h1>Contact us!</h1>
<p>
This is the how
to contact us.
</p>
</main>
{% endblock %}
_base.html
because they each extend _base.html
_base.html
with the corresponding blocks in index.html
, about.html
and contact.html
The browser does not fetch templates and then combine them.
The server does not take templates and combine them.
staticjinja is a quite separate program.
It combines templates.
Then its output (web pages) can be placed on the server.
Browsers and servers work as normal.
They know nothing about templates.
{% include %}
to include it in a web page.CSS frameworks
Writing CSS to create a visually appealing web site is difficult.
Writing CSS for a visually appealing, responsive web site is even more difficult.
Making sure that the web site is also accessible is even more difficult.
Prewritten CSS for multi-column sites,
usually liquid, sometimes RWD.
E.g. Bootstrap
Mobile-first and RWD.
Up to 12 equal-sized columns, but you can combine them.
You may need to put some extra div
s into your HTML.
You will need to put class attributes onto the elements in your HTML.
You can customize the colours and so on.
One way to use Bootstrap:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Welcome!</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css"
integrity="sha384-JcKb8q3iqJ61gNV9KGb8thSsNjpSL0n8PARn9HuZOnIxN0hoP+VmmDGMN5t9UJ0Z" crossorigin="anonymous" />
</head>
<body>
<-- Your stuff goes here -->
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"
integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj"
crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"
integrity="sha384-9/reFTGAW83EW2RDu2S0VKaIzap3H66lZH81PoYlFhbGU+6BZp6G7niu735Sk7lN"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/js/bootstrap.min.js"
integrity="sha384-B4gt1jrGC7Jh4AgTPSdUtOBvfO8shuf57BaghqFfPlYxofvL8/KUEfYiJOMMV+rV"
crossorigin="anonymous"></script>
</body>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Welcome!</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/5.0.0-alpha1/css/bootstrap.min.css" integrity="sha384-r4NyP46KrjDleawBgD5tp8Y7UzmLA05oM1iAEQ17CSuDqnUK2+k9luXQOfXJCJ4I" crossorigin="anonymous" />
</head>
<body>
<header>
Header
</header>
<div class="row" role="presentation">
<nav class="col-sm-4">
Nav
</nav>
<main class="col-sm-8">
Main
</main>
</div>
<footer>
Footer
</footer>
</body>
</html>
div
s and class attribtes (class-itis!) for presentational
purposes;
development
and
testing
and
deployment
and
monitoring
Web developers write HTML and CSS using an editor or an IDE (e.g. Visual Studio Code).
They test in a browser.
Methods where there is no server and no HTTP request/response:
These methods do not fully test your web page.
Methods that do use a server:
After testing comes deployment:
moving the web site from the development machine to the production server.
After deployment comes monitoring:
measure performance to decide whether changes are needed.
Companies with a scientific approach to doing business will test everything about their web site with real users.
One approach is A/B testing:
assign visitors to your site into group A or group B at random;
show one version of the site to group A and a different version to group B
measure clicks or sales or…
Should we use a 'hamburger icon' or the word MENU?
What shade of blue should Google use in its search results?
Our study of Web Development in the case of static web sites ends here.
But, we will resume with a study of Web Development in the case of dynamic web sites.