2

I have a NavHandler to pass json data for the nav page. I want to include the nav to the base so all the pages can see the navigation.

I can see the JSON data in nav.html but it does not show up in base.html and index.html.

May I know how to pass variables to all templates?

What did I miss out?

Besides that I have also followed the Jinja2 sample codes to test the on @app.context_processor and the nav.html, but given undefined variable.

main.py

from flask import Flask app = Flask(__name__) @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) class NavHandler(weapp2.RequestHandler): def get(self): json_str = '''[{"name":"Nav A", "link":"link 1"},{"name":"Nav B", "link":"link 2"},{"name":"Nav C", "link":"link 3"}]''' json_data = json.load(json_str) template_vars = json_data template = JINJA_ENVIRONMENT.get.template('nav.html') self.response.write(template.render(template_vars, jason_data=json=data)) class BaseHandler(weapp2.RequestHandler): def get(self): template_vars = { 'title' : title} template = JINJA_ENVIRONMENT.get.template('nav.html') self.response.write(template.render(template_vars)) 

nav.html

{% block nav %} <ul> {% for d from json_data %} <li><a href="{{ d.link }}">{{ d.name }}</a> {% endfor %} </ul> <script> console.log({{ format_price(0.33) }}); </script> { endblock %} 

base.html

<!doctype html> <html> <head> <meta charset="utf-8"/> <title>{% block title %} - My Site</title> </head> <body> <div>Navbar</div> {% include nav.html %} {% block content %}{% endblock %} </body> </html> 

index.html

{% extends 'base.html' %} {% block content %} <h3>{% block title %}Home{% endblock %}</h3> <p>Hello, World!</p> {% endblock %} 

1 Answer 1

3

You can use a context processor to inject global variables in templates.

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.

Checkout this question and this answer, they probably can help you.

Edit: for the new question update

Change this line:

return dict(format_price=g.format_price)

for this one:

return dict(format_price=format_price)

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

5 Comments

So I have to import flask?
I have installed flask into app lib, then I follow the Jinja example here: flask.pocoo.org/docs/0.10/templating/#context-processors When I tried {{ format_price(0.33) }} in a template 'nav.html', it threw me the following error: UndefinedError: 'format_price' is undefined. Any idea?
@dnez no ideas without looking at the code, but for that I may suggest you to make another question focusing the scope on context processors, otherwise update the body of this question.
@dnez check the edit on the answer and see if thar works for you.
removed g. still 'UndefinedError: 'format_price' is undefined'

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.