Flask 8 – Templates

This time we are gonna see how to use the same ‘style’ for all the pages of our blog.

The template.html page, a base for all pages

<html>

    <style>
    
    body {
    margin-left: 10%;
    margin-right: 10%;
    font-size: 2em;
    color: coral;
    background-color: black;
    }
    h3 {
        color: white;
        background-color: coral;
    }
    </style>
    <body>

    {% block page %}
    {% endblock %}



</body></html>

We added the {% block page %} and {% endblock %} to the base page, called template.html, so that the ‘decoration’ of every page will be this page, while the content will be in each page of the site like in the following example of the homepage.html

{% extends 'template.html' %}

{% block page %}

<h1>Welcome to our Homepage</h1>

{% for post in posts %}
    <h3> {{ post.title }}</h3>
    <p>{{ post.body }}</p>

    {% if author %}
    <span style="font-size:0.5em;color:yellowgreen">{{ post.author }} </span>
    {% endif %}
    
    {% if date %}
    <span style="font-size:0.5em;color:yellowgreen">{{ post.date }}</span>
    {% endif %}
 {% endfor %}

{% endblock %}

Now, take a look at the video to have a better comprehension about how it works.

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.