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 %}