Global variables in Flask templates

I assume your Flaskobject’s name is app (i.e., app = Flask(__name__)).

Place the below code right after the app is initialized.

@app.context_processor
def inject_stage_and_region():
    return dict(stage="alpha", region="NA")

In your Jinja templates, "alpha"and "NA" can be referenced by {{ stage }} and {{ region }}.

Flask docs:
http://flask.pocoo.org/docs/0.12/templating/#context-processors

To inject new variables automatically into the context of a template, context processors exist in Flask. Context processors run before the template is rendered and have the ability to inject new values into the template context. A context processor is a function that returns a dictionary. The keys and values of this dictionary are then merged with the template context, for all templates in the app

Leave a Comment