Skip to content

Instantly share code, notes, and snippets.

@rizkyabdilah
Created June 9, 2012 06:44
Show Gist options
  • Save rizkyabdilah/2899860 to your computer and use it in GitHub Desktop.
Save rizkyabdilah/2899860 to your computer and use it in GitHub Desktop.
Set global_env on Jinja2Template Class
#!/opt/python/bin/python
class Jinja2Template(BaseTemplate):
global_env = None
def prepare(self, filters=None, tests=None, **kwargs):
from jinja2 import Environment, FunctionLoader
if 'prefix' in kwargs: # TODO: to be removed after a while
raise RuntimeError('The keyword argument `prefix` has been removed. '
'Use the full jinja2 environment name line_statement_prefix instead.')
self.env = Environment(loader=FunctionLoader(self.loader), **kwargs)
if self.global_env:
self.env.globals.update(global_env)
if filters: self.env.filters.update(filters)
if tests: self.env.tests.update(tests)
if self.source:
self.tpl = self.env.from_string(self.source)
else:
self.tpl = self.env.get_template(self.filename)
## end class Jinja2Template
## example how to set,
from bottle import Jinja2Template
Jinja2Template.global_env = {'gob', 'this_is_global_variable'}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment