Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bottle py and Jinja2 global variable

I am using bottle.py framework along with Jinja2 templates in a new application.

Whenever a user logs into the application I would like to add a new global Jinja2 variable with the name of the active user, so in a partial template (the header) I can display this name.

In my python (bottle) file I've tried several things according to what I googled so far, but no success yet.

This is the last thing I tried inside a function:

import jinja2
env = jinja2.Environment(loader=jinja2.FileSystemLoader('templates'))
env.globals['myglobal'] = 'My global'
#env.globals.update({'myglobal': 'My global'})

But, when putting this into the header template: {{myglobal}} it simply doesn't show up.

Since this is my first time with bottle and jinja2 anyone knows how can achieve this?

like image 582
Roland Pish Avatar asked May 26 '26 08:05

Roland Pish


1 Answers

I have a similar setup, and this works for me using the BaseTemplate built into bottle:

from bottle import BaseTemplate
BaseTemplate.defaults['myglobal'] = 'My global'
like image 103
Dan Gayle Avatar answered Jun 01 '26 19:06

Dan Gayle