Flask 7 – The if condition

The if condition in the html page to get data and condition depending on routes.py data. It is very useful to make some elements of the page to be visible or not depending on boolean variables that are stored in the routes.py file. In the following video it will be shown a way you can use this at your advantage.

the routers.py file

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",
        "author": "Giovanni Pyhton",
        "date": "06/07/2019"},

        {"title": "Post. nr.2",
        "body": "Hello... we're back, with the second post",
        "author": "Giovanni Pyhton",
        "date": "06/07/2019"}
    ]

    author = True
    date = False


    return render_template("homepage.html", posts=posts, author=author, date=date)

 

 

The homepage.html

<html>

<style>

body {
color: coral;
background-color: black;

}

</style>

<body>

<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 %}



</body>


</html>

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.