Base template

Template Inheritance flask


Usually regular sites have parts of the page shared between all pages. It can be header, footer, head block with required page setup information, main menu and auth section. Jinja templating system should help us to implement it.

First of all let's remove standard Angular Page generated for new project. We don't need this nice page anymore. Let's replace it with simple placeholder.

./frontend/src/app/app.component.html
<div>
    <h1>Angular APP</h1>
</div>

In ./templates folder we need to create new file with base template

./backend/templates/_base.html
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Salesforce Backup Service</title>

    {% block header %}{% endblock %}

</head>
<body>

    <div class="main-wrapper">
        <div class="c-header">
            <h1>Salesforce Backup Service</h1>
        </div>

        <hr />

        <div class="c-body">
            {% block content %}{% endblock %}
        </div>

        <hr />

    </div>

    <footer style="text-align: center;">
        <p>&copy; 2020 - Salesforce Backup Service</p>
    </footer>

</body>
</html>

Here you can see lines:
{% block header %}{% endblock %}
and
{% block content %}{% endblock %}
It is placeholders where we should insert content from others templates.

To make it works we need to modify home.html and login.html templates

./backend/templates/home.html
{% extends "_base.html" %}

{% block header %}
    {% if is_development_mode %}
        <base href="/">
    {% else %}
        {{ resources.head | safe }}
    {% endif %}
{% endblock %}

{% block content %}
    {% if is_development_mode %}
        <div>
            <script>
                console.log('************************************');
                console.log('************************************');
                console.log('         DEVELOPMENT MODE');
                console.log('************************************');
                console.log('************************************');
            </script>
            <div style="position: fixed; top: 0; right: 0; background-color: #ef4646; color: #fff; padding: 0 7px; z-index: 99999;">DEVELOPMENT MODE</div>
        </div>
        <app-root></app-root>
        <script src="https://localhost:4200/runtime.js" type="module"></script>
        <script src="https://localhost:4200/polyfills.js" type="module"></script>
        <script src="https://localhost:4200/styles.js" type="module"></script>
        <script src="https://localhost:4200/vendor.js" type="module"></script>
        <script src="https://localhost:4200/main.js" type="module"></script>
    {% else %}
        <h2>PRODUCTION MODE</h2>
        <app-root></app-root>
        {{ resources.scripts | safe }}
    {% endif %}
{% endblock %}


./backend/templates/auth/login.html
{% extends "_base.html" %}

{% block content %}
<div>
    <h3>Login</h3>
    <div>
        {% with messages = get_flashed_messages() %}
            {% if messages %}
                <div class="notification is-danger">
                    {{ messages[0] }}
                </div>
            {% endif %}
        {% endwith %}
        <form method="POST" action="">
            <div>
                <label>Email</label>
                <input type="email" name="email" placeholder="Your Email" />
            </div>

            <div>
                <label>Password</label>
                <input type="password" name="password" placeholder="Your Password" />
            </div>

            <div>
                <label class="checkbox">
                    <input type="checkbox">
                    Remember me
                </label>
            </div>

            <button>Login</button>

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

The main pages structure remains the same instead of few new Jinja2 tags: extends and block
More about Jinja2 Templating System you can read here
https://jinja.palletsprojects.com/en/2.11.x/templates/#template-inheritance


Angular App inside Flask Jinja2 Base template





Comments

Popular posts from this blog

HTTPS in local environment for Angular + Flask project.

Salesforce Authentication 2 (Token Validation and Refresh)

User authentification with Flask-Login