Search code examples
pythonhtmltornadohttphandler

handler to generate global variables in tornado


I have a top bar in my web application that is common to all the pages. The code for that is in a separate html file, called,base.html that's included in all the other pages(ex. sample.html). Now, I want to load a variable 'notifications' in base.html. I would like to use a separate Handler if possible to generate the notifications for base.html. However, currently, as I understand, I have to call a def to generate it from every page Handler that I write/have written and pass those on. Is there a cleaner way to do this whereby I don't have to change the other page handlers ? Sorry,if this is a stupid question. I'm fairly new to tornado.

base.html

// some html 
{{ notifications  }}


sample.html

{% extends base.html %}

// some html

Solution

  • The way to do this is with a UIModule. Create a subclass of UIModule which defines a render method, pass it in the ui_modules dict to the Application constructor (ui_modules=dict(Notifications=NotificationsModule)), and then you can call it in your templates with {% module Notifications() %}