diff --git a/more/jinja2/main.py b/more/jinja2/main.py index d77a1e4..f41d680 100644 --- a/more/jinja2/main.py +++ b/more/jinja2/main.py @@ -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 diff --git a/more/jinja2/tests/fixtures/template_autoreload.py b/more/jinja2/tests/fixtures/template_autoreload.py new file mode 100644 index 0000000..f351cce --- /dev/null +++ b/more/jinja2/tests/fixtures/template_autoreload.py @@ -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} diff --git a/more/jinja2/tests/test_jinja2.py b/more/jinja2/tests/test_jinja2.py index e780587..7dfbfec 100644 --- a/more/jinja2/tests/test_jinja2.py +++ b/more/jinja2/tests/test_jinja2.py @@ -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(): @@ -82,3 +84,46 @@ def test_override_template_inheritance(): ''' + + +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'''\ + + +

Hello world!

+ +''' + + with open(autoreload_path, mode='w') as template: + for line in template_lines: + if 'Hello' in line: + line += '

Autoreload Test

\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'''\ + + +

Hello world!

+

Autoreload Test

+ +''' + + os.unlink(autoreload_path)