Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject config models and globals into Jinja2 globals. #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion more/jinja2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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')
Expand Down
33 changes: 33 additions & 0 deletions more/jinja2/tests/fixtures/inject_globals.py
Original file line number Diff line number Diff line change
@@ -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}
28 changes: 28 additions & 0 deletions more/jinja2/tests/fixtures/inject_models.py
Original file line number Diff line number Diff line change
@@ -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}
6 changes: 6 additions & 0 deletions more/jinja2/tests/fixtures/templates/person_globals.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<p>Hello {{name}}!</p>
<p>{{ fancy_function() }}</p>
</body>
</html>
6 changes: 6 additions & 0 deletions more/jinja2/tests/fixtures/templates/person_link.jinja2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<html>
<body>
<p>Hello {{name}}!</p>
<p><a href="{{ request.link(Person('mars')) }}">Mars</a></p>
</body>
</html>
28 changes: 27 additions & 1 deletion more/jinja2/tests/test_jinja2.py
Original file line number Diff line number Diff line change
@@ -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():
Expand Down Expand Up @@ -82,3 +82,29 @@ def test_override_template_inheritance():
</div>
</body>
</html>'''


def test_inject_globals():
c = Client(inject_globals.App())

response = c.get('/persons/world')
assert response.body == b'''\
<html>
<body>
<p>Hello world!</p>
<p>This is fancy</p>
</body>
</html>'''


def test_inject_models():
c = Client(inject_models.App())

response = c.get('/persons/world')
assert response.body == b'''\
<html>
<body>
<p>Hello world!</p>
<p><a href="http://localhost/persons/mars">Mars</a></p>
</body>
</html>'''