These are the codes I've wrote:
application.py:
from flask import Flask, render_template
app = Flask(__name__)
@app.context_processor
def inject_foo():
return dict(foo='bar')
@app.route('/')
def index():
return render_template('index.html')
index.html:
{% from 'macros.html' import print_foo %}
<p>print_foo: {{ print_foo() }}</p>
<p>foo(directly): {{ foo }}</p>
macros.html:
{% macro print_foo() %}
foo is {{ foo }}
{% endmacro %}
And the files are structured like this:
flask-context-processor-issue/
application.py
templates/
index.html
macros.html
When I run the application(through flask run) and go to http://localhost:5000, I see this page:
print_foo: foo is
foo(directly): bar
foo for print_foo is missing. I think the reason is that foo is not visible inside the print_foo macro(which is in an imported template).
I can force foo to be globally visible by modify the application.py:
...
# @app.context_processor
# def inject_foo():
# return dict(foo='bar')
app.jinja_env.globals['foo'] = 'bar'
...
But working along with app.context_processor is much general way I think, so I wonder is there any way to make the example works(with app.context_processor).
Waiting for your answer, thanks.