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

Load template on render to allow a chance for auto reloading #11

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
3 changes: 2 additions & 1 deletion more/jinja2/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ def get_jinja2_loader(template_directories, settings):

@Jinja2App.template_render(extension='.jinja2')
def get_jinja2_render(loader, name, original_render):
template = loader.get_template(name)

def render(content, request):
template = loader.get_template(name)
variables = {'request': request}
variables.update(content)
return original_render(template.render(**variables), request)

return render
29 changes: 29 additions & 0 deletions more/jinja2/tests/fixtures/template_autoreload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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 {
'auto_reload': True,
}


# note "person_autoreload.jinja2" will be created (and deleted) by the test
@App.html(model=Person, template='person_autoreload.jinja2')
def person_default(self, request):
return {'name': self.name}
47 changes: 46 additions & 1 deletion more/jinja2/tests/test_jinja2.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import os

from webtest import TestApp as Client
from .fixtures import (
template, template_inheritance, override_template,
override_template_inheritance)
override_template_inheritance, template_autoreload)


def test_template():
Expand Down Expand Up @@ -82,3 +84,46 @@ def test_override_template_inheritance():
</div>
</body>
</html>'''


def test_autoreload_template():
templates_path = os.path.join(
os.path.dirname(__file__), 'fixtures', 'templates')
with open(os.path.join(templates_path, 'person.jinja2')) as template:
template_lines = template.readlines()

autoreload_path = os.path.join(templates_path, 'person_autoreload.jinja2')
with open(autoreload_path, mode='w') as template:
template.writelines(template_lines)
timestamp = os.path.getmtime(autoreload_path)

c = Client(template_autoreload.App())

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

with open(autoreload_path, mode='w') as template:
for line in template_lines:
if 'Hello' in line:
line += '<p>Autoreload Test</p>\n'
template.write(line)

# alter modification time as required by the jinja2 FileSystemLoader
# https://github.com/pallets/jinja/blob/master/jinja2/loaders.py#L179-L187
os.utime(autoreload_path, (timestamp, timestamp + 1))

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

os.unlink(autoreload_path)