Templates

With the style assets in place and served by the application, the CSS template download also includes HTML files for various pages, here they’ll be adapted into Jinja2 templates.

Jinja2 is a popular and powerful templating engine for Python web applications. Templating allows developers to separate the presentation of data from its logic, making it easier to maintain and update code. With Jinja2, developers can create dynamic HTML, XML, or other markup languages, by defining templates that include placeholders for dynamic data. These placeholders are called variables, filled with values at runtime. Jinja2 also provides a range of control structures, such as loops and conditional statements, which allow developers to add logic to their templates, making creating more complex and flexible templates possible. Jinja2 allows for template inheritance, where one template can extend another and override specific blocks. This makes it possible to reuse and organize templates more efficiently. Overall, Jinja2 is a versatile and powerful tool for creating dynamic web applications and is widely used in the Python community.

Create a directory to keep templates:

mkdir -p web/templates

When working with templates, it’s good practice to break them up to reduce duplicate HTML. For example the site navigation can live in a base template, meaning you only need to change site wide elements in one place.

Here is how you break up the theme’s index.html into two templates, _base.html and home.html. The base template holds everything common to every page (the <head>, navigation, footer and scripts), and leaves a {% block content %} for each page to fill in:

web/templates/_base.htmljinja
<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">

    <title>ExampleForYou</title>

    <link rel="shortcut icon" href="/static/favicon.ico">

    <!-- Bootstrap Core CSS -->
    <link href="/static/vendor/bootstrap/css/bootstrap.min.css" rel="stylesheet">

    <!-- Theme CSS -->
    <link href="/static/css/clean-blog.min.css" rel="stylesheet">

    <!-- Custom Fonts -->
    <link href="/static/vendor/font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <link href='https://fonts.googleapis.com/css?family=Lora:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
    <link href='https://fonts.googleapis.com/css?family=Open+Sans:300italic,400italic,600italic,700italic,800italic,400,300,600,700,800' rel='stylesheet' type='text/css'>

</head>

<body>

    <!-- Navigation -->
    <nav class="navbar navbar-default navbar-custom navbar-fixed-top">
        <div class="container-fluid">
            <!-- Brand and toggle get grouped for better mobile display -->
            <div class="navbar-header page-scroll">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
                    <span class="sr-only">Toggle navigation</span>
                    Menu <i class="fa fa-bars"></i>
                </button>
                <a class="navbar-brand" href="/">ExampleForYou</a>
            </div>

            <!-- Collect the nav links, forms, and other content for toggling -->
            <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
                <ul class="nav navbar-nav navbar-right">
                    <li>
                        <a href="/">Home</a>
                    </li>
                    <li>
                        <a href="/blog/create">Create</a>
                    </li>
                </ul>
            </div>
            <!-- /.navbar-collapse -->
        </div>
        <!-- /.container -->
    </nav>

    {% block content %}
    {% endblock %}

    <hr>

    <!-- Footer -->
    <footer>
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    <ul class="list-inline text-center">
                        <li>
                            <a href="#">
                                <span class="fa-stack fa-lg">
                                    <i class="fa fa-circle fa-stack-2x"></i>
                                    <i class="fa fa-github fa-stack-1x fa-inverse"></i>
                                </span>
                            </a>
                        </li>
                    </ul>
                    <p class="copyright text-muted">Copyright &copy; ExampleForYou</p>
                </div>
            </div>
        </div>
    </footer>

    <!-- jQuery -->
    <script src="/static/vendor/jquery/jquery.min.js"></script>

    <!-- Bootstrap Core JavaScript -->
    <script src="/static/vendor/bootstrap/js/bootstrap.min.js"></script>

    <!-- Theme JavaScript -->
    <script src="/static/js/clean-blog.min.js"></script>

</body>

</html>
web/templates/home.htmljinja
{% extends "_base.html" %}

{% block content %}

    <!-- Page Header -->
    <!-- Set your background image for this header on the line below. -->
    <header class="intro-header" style="background-image: url('/static/img/home-bg.jpg')">
        <div class="container">
            <div class="row">
                <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">
                    <div class="site-heading">
                        <h1>ExampleForYou</h1>
                        <hr class="small">
                        <span class="subheading">A blog built with FastAPI</span>
                    </div>
                </div>
            </div>
        </div>
    </header>

    <!-- Main Content -->
    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2 col-md-10 col-md-offset-1">

            {% for post in context.blogposts %}
                <div class="post-preview">
                    <a href="/blog/{{ post.id }}">
                    <h2 class="post-title">
                        {{ post.title }}
                    </h2>
                    <h3 class="post-subtitle">
                        {{ post.teaser }}
                    </h3>
                    </a>
                    <p class="post-meta">Posted on {{ post.created_date.strftime('%d %B %Y') }}</p>
                </div>
            <hr>
            {% endfor %}

            </div>
        </div>
    </div>
{% endblock %}

home.html starts with {% extends "_base.html" %} and only defines the content block. The loop over context.blogposts will display the latest posts, once the next section passes them in. strftime('%d %B %Y') formats the date as, for example, 02 December 2026, rather than the raw 2026-12-02 14:12:43.769610+00:00.

note

The navigation links to /blog/create for now. In the Authentication section it changes, to show different links depending on whether someone is logged in.