diff options
| -rw-r--r-- | CHANGES.txt | 7 | ||||
| -rw-r--r-- | pyramid/chameleon_zpt.py | 22 | ||||
| -rw-r--r-- | pyramid/tests/test_chameleon_zpt.py | 12 |
3 files changed, 33 insertions, 8 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 41ecbde0f..6cb2932fa 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -154,6 +154,13 @@ Features Bug Fixes --------- +- When the ``pyramid.reload_templates`` setting was true, and a Chameleon + template was reloaded, and the renderer specification named a macro + (e.g. ``foo#macroname.pt``), renderings of the template after the template + was reloaded due to a file change would produce the entire template body + instead of just a rendering of the macro. See + https://github.com/Pylons/pyramid/issues/1013. + - Fix an obscure problem when combining a virtual root with a route with a ``*traverse`` in its pattern. Now the traversal path generated in such a configuration will be correct, instead of an element missing diff --git a/pyramid/chameleon_zpt.py b/pyramid/chameleon_zpt.py index 89e5d02b5..4ea5d506d 100644 --- a/pyramid/chameleon_zpt.py +++ b/pyramid/chameleon_zpt.py @@ -4,9 +4,20 @@ from pyramid.interfaces import ITemplateRenderer from pyramid.decorator import reify from pyramid import renderers +from chameleon.zpt.template import PageTemplateFile + def renderer_factory(info): return renderers.template_renderer_factory(info, ZPTTemplateRenderer) +class PyramidPageTemplateFile(PageTemplateFile): + def cook(self, body): + PageTemplateFile.cook(self, body) + if self.macro: + # render only the portion of the template included in a + # define-macro named the value of self.macro + macro_renderer = self.macros[self.macro].include + self._render = macro_renderer + @implementer(ITemplateRenderer) class ZPTTemplateRenderer(object): def __init__(self, path, lookup, macro=None): @@ -16,18 +27,13 @@ class ZPTTemplateRenderer(object): @reify # avoid looking up reload_templates before manager pushed def template(self): - from chameleon.zpt.template import PageTemplateFile - tf = PageTemplateFile( + tf = PyramidPageTemplateFile( self.path, auto_reload=self.lookup.auto_reload, debug=self.lookup.debug, - translate=self.lookup.translate + translate=self.lookup.translate, + macro=self.macro, ) - if self.macro: - # render only the portion of the template included in a - # define-macro named the value of self.macro - macro_renderer = tf.macros[self.macro].include - tf._render = macro_renderer return tf def implementation(self): diff --git a/pyramid/tests/test_chameleon_zpt.py b/pyramid/tests/test_chameleon_zpt.py index 5ac57f869..d7ca94298 100644 --- a/pyramid/tests/test_chameleon_zpt.py +++ b/pyramid/tests/test_chameleon_zpt.py @@ -140,6 +140,18 @@ class ZPTTemplateRendererTests(Base, unittest.TestCase): self.assertEqual(result, '<html>\nOutside macro\n\n Hello!\n\n</html>\n\n') + def test_macro_template_reload(self): + minimal = self._getTemplatePath('withmacro.pt') + lookup = DummyLookup() + instance = self._makeOne(minimal, lookup, macro='foo') + result = instance.implementation()() + self.assertEqual(result, '\n Hello!\n') + instance.template.cook( + '<html>\nOutside macro\n\n Hello!\n\n</html>\n\n' + ) + result = instance.implementation()() + self.assertEqual(result, '\n Hello!\n') + class DummyLookup(object): auto_reload=True debug = True |
