Flask 6 – Jinga2 and code into html pages

Now we come to the exciting stuff… the jinga2 code language to use the python’s code into html. Here we can start to see the advantages of flask.

The changes in routes.py

from flask import render_template
from blog import app

@app.route("/")
def homepage():
    posts = [
        
        {"title": "Post nr. 1",
        "body": "This is the first post of my blog"},

        {"title": "Post. nr.2",
        "body": "Hello... we're back, with the second post"}

    ]
    return render_template("homepage.html", posts=posts)

The code in homepage.html

<body>

<h1>Welcome to our Homepage</h1>

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


</body>

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.