1

Some data needs to be present in all templates.

How can I transfer the data I need to base.html?

For normal templates I use the render() function.

I do not understand how to apply this for the base template, as it does not get passed into render()

base.html

<html>
    <head>
    </head>
    <header>Your balance: {{ balance }}</header>
<body> 
{% block content %}
{% endblock %}
</body>
</html>

views.py

@app.route("/")
def index():
    balance = User.query.get(...)
    return render_template('index.html',
    balance = balance)

@app.route("/settings")
def settings():
    balance = User.query.get(...)
    return render_template('settings.html',
    balance = balance)

@app.route("/exchange")
def exchange():
    balance = User.query.get(...)
    return render_template('exchange.html',
    balance = balance)

etc ..

{{ balance }} - It should be displayed on all pages where this base.html is enabled.

To do this, I need to pass a balance object in each function for each page. How can I make it so that I can only transfer it once for all pages?

1
  • Hi, would you mind adding some details or code to show us what you've tried and what hasn't worked, and what your desired output is. Commented Oct 8, 2020 at 21:10

2 Answers 2

5

I am late to answer but for the benefit of others I am adding this. flask context_processor would help. You can pass your function to the decorator anywhere in your file and then, you can call the function from any of your html templates.

@app.context_processor
def utility_processor():
    def format_price(amount, currency=u'€'):
        return u'{0:.2f}{1}'.format(amount, currency)
    return dict(format_price=format_price)

The context processor above makes the format_price function available to all templates:

The above function output can be called by {{ format_price(0.33) }} in your html file.

Official Document: https://flask.palletsprojects.com/en/1.1.x/templating/#context-processors

Sign up to request clarification or add additional context in comments.

Comments

2

The base.html file is meant to serve as a template for your other HTML files. You do not require to render it as a template from your app. Instead, you include base.html as an extension to your other HTML files.

There is no restriction is displaying variables in the base.html file. The below example should be of help.

base.html

<html>
    <head>
        <title>{{ title }}</title>
    </head>
<body> 

{% block content %}
 
{% endblock %}

</body>
</html>

index.html

{% extends "base.html" %}
{% block content %}
<h1>Homepage</h1>
{% endblock %}

app.py

from flask import Flask,render_template
app = Flask(__name__)

@app.route("/")
def index():
    title = "Homepage"
    
    return render_template('index.html',
    title = title)


if __name__ == '__main__':
    app.run(debug=True)

4 Comments

That's the problem. You need to pass a title for each page. It's inconvenient and code repetition. And I need to pass it on once for the whole site. In Django, for example, it's called context_processor
There is context processor for Jinja2 too - flask.palletsprojects.com/en/1.1.x/templating/…
Also, there are some objects that are available to any template. current_user from Flask-Login is one of those, so if balance is current_user.balance, you can use it in any template.
I'd take care. current_user maybe be None is no the user is not logged in yet. That is why I do not like the whole idea of user data in the base template - but that's just me :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.