From f2e35ac1544b383cc6d79606f04dd71996bf599c Mon Sep 17 00:00:00 2001 From: Yeray Diaz Diaz Date: Mon, 1 Jan 2018 13:43:33 +0000 Subject: [PATCH] Inject config `models` and `globals` into Jinja2 globals. --- more/jinja2/main.py | 10 +++++- more/jinja2/tests/fixtures/inject_globals.py | 33 +++++++++++++++++++ more/jinja2/tests/fixtures/inject_models.py | 28 ++++++++++++++++ .../fixtures/templates/person_globals.jinja2 | 6 ++++ .../fixtures/templates/person_link.jinja2 | 6 ++++ more/jinja2/tests/test_jinja2.py | 28 +++++++++++++++- 6 files changed, 109 insertions(+), 2 deletions(-) create mode 100644 more/jinja2/tests/fixtures/inject_globals.py create mode 100644 more/jinja2/tests/fixtures/inject_models.py create mode 100644 more/jinja2/tests/fixtures/templates/person_globals.jinja2 create mode 100644 more/jinja2/tests/fixtures/templates/person_link.jinja2 diff --git a/more/jinja2/main.py b/more/jinja2/main.py index d77a1e4..6835ade 100644 --- a/more/jinja2/main.py +++ b/more/jinja2/main.py @@ -16,6 +16,12 @@ def get_setting_section(): @Jinja2App.template_loader(extension='.jinja2') def get_jinja2_loader(template_directories, settings): config = settings.jinja2.__dict__.copy() + _globals = config.pop('globals', {}) + _models = config.pop('models', {}) + + _globals.update({ + model.__name__: model for model in _models + }) # we always want to use autoescape as this is about # HTML templating @@ -24,9 +30,11 @@ def get_jinja2_loader(template_directories, settings): 'extensions': ['jinja2.ext.autoescape'] }) - return jinja2.Environment( + env = jinja2.Environment( loader=jinja2.FileSystemLoader(template_directories), **config) + env.globals.update(_globals) + return env @Jinja2App.template_render(extension='.jinja2') diff --git a/more/jinja2/tests/fixtures/inject_globals.py b/more/jinja2/tests/fixtures/inject_globals.py new file mode 100644 index 0000000..46130c7 --- /dev/null +++ b/more/jinja2/tests/fixtures/inject_globals.py @@ -0,0 +1,33 @@ +from more.jinja2 import Jinja2App + + +class App(Jinja2App): + pass + + +@App.path(path='persons/{name}') +class Person(object): + def __init__(self, name): + self.name = name + + +@App.template_directory() +def get_template_dir(): + return 'templates' + + +@App.setting_section(section='jinja2') +def get_setting_section(): + def fancy_function(): + return 'This is fancy' + + return { + 'globals': { + 'fancy_function': fancy_function + } + } + + +@App.html(model=Person, template='person_globals.jinja2') +def person_default(self, request): + return {'name': self.name} diff --git a/more/jinja2/tests/fixtures/inject_models.py b/more/jinja2/tests/fixtures/inject_models.py new file mode 100644 index 0000000..9eee1eb --- /dev/null +++ b/more/jinja2/tests/fixtures/inject_models.py @@ -0,0 +1,28 @@ +from more.jinja2 import Jinja2App + + +class App(Jinja2App): + pass + + +@App.path(path='persons/{name}') +class Person(object): + def __init__(self, name): + self.name = name + + +@App.template_directory() +def get_template_dir(): + return 'templates' + + +@App.setting_section(section='jinja2') +def get_setting_section(): + return { + 'models': [Person] + } + + +@App.html(model=Person, template='person_link.jinja2') +def person_default(self, request): + return {'name': self.name} diff --git a/more/jinja2/tests/fixtures/templates/person_globals.jinja2 b/more/jinja2/tests/fixtures/templates/person_globals.jinja2 new file mode 100644 index 0000000..4f3d734 --- /dev/null +++ b/more/jinja2/tests/fixtures/templates/person_globals.jinja2 @@ -0,0 +1,6 @@ + + +

Hello {{name}}!

+

{{ fancy_function() }}

+ + diff --git a/more/jinja2/tests/fixtures/templates/person_link.jinja2 b/more/jinja2/tests/fixtures/templates/person_link.jinja2 new file mode 100644 index 0000000..0d4c4aa --- /dev/null +++ b/more/jinja2/tests/fixtures/templates/person_link.jinja2 @@ -0,0 +1,6 @@ + + +

Hello {{name}}!

+

Mars

+ + diff --git a/more/jinja2/tests/test_jinja2.py b/more/jinja2/tests/test_jinja2.py index e780587..c1125c6 100644 --- a/more/jinja2/tests/test_jinja2.py +++ b/more/jinja2/tests/test_jinja2.py @@ -1,7 +1,7 @@ from webtest import TestApp as Client from .fixtures import ( template, template_inheritance, override_template, - override_template_inheritance) + override_template_inheritance, inject_models, inject_globals) def test_template(): @@ -82,3 +82,29 @@ def test_override_template_inheritance(): ''' + + +def test_inject_globals(): + c = Client(inject_globals.App()) + + response = c.get('/persons/world') + assert response.body == b'''\ + + +

Hello world!

+

This is fancy

+ +''' + + +def test_inject_models(): + c = Client(inject_models.App()) + + response = c.get('/persons/world') + assert response.body == b'''\ + + +

Hello world!

+

Mars

+ +'''