summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMalthe Borch <mborch@gmail.com>2011-09-13 11:15:39 +0000
committerMalthe Borch <mborch@gmail.com>2011-09-13 11:15:39 +0000
commit36e89981644e0ae93a52a1819ded849c9c524ff0 (patch)
tree4191de0435a6fdf38f6e946433dba044e4be6e52 /src
parent0552c5d9c7495ee08d75e33e36137b114c5611c2 (diff)
downloadzope-pagetemplate-36e89981644e0ae93a52a1819ded849c9c524ff0.tar.gz
Fixed issue where a template would not have a ``_v_macros`` attribute.
This volatile attribute is relied upon by some legacy code such as the Zope 2 product ``PageTemplates``. In this changeset, the interface is changed to match that of the TAL parser's ``getCode`` method which returns a tuple ``(program, macros)``. These are in turn assigned to the volatile attributes ``_v_program`` and ``_v_macros``.
Diffstat (limited to 'src')
-rw-r--r--src/zope/pagetemplate/interfaces.py9
-rw-r--r--src/zope/pagetemplate/pagetemplate.py18
-rw-r--r--src/zope/pagetemplate/tests/test_basictemplate.py9
3 files changed, 20 insertions, 16 deletions
diff --git a/src/zope/pagetemplate/interfaces.py b/src/zope/pagetemplate/interfaces.py
index 19cbaf7..4929b39 100644
--- a/src/zope/pagetemplate/interfaces.py
+++ b/src/zope/pagetemplate/interfaces.py
@@ -113,23 +113,22 @@ class IPageTemplateEngine(Interface):
"""
def cook(source_file, text, engine, content_type):
- """Parse text and return prepared template program.
+ """Parse text and return prepared template program and macros.
Note that while ``source_file`` is provided to name the source
of the input ``text``, it should not be relied on to be an
actual filename (it may be an application-specific, virtual
path).
+
+ The return type is a tuple ``(program, macros)``.
"""
class IPageTemplateProgram(Interface):
"""Cooked template program."""
- macros = Attribute(
- "Template macros.")
-
def __call__(
- context, debug=0, wrap=60, metal=1, tal=1, showtal=-1,
+ context, macros, debug=0, wrap=60, metal=1, tal=1, showtal=-1,
strictinsert=1, stackLimit=100, i18nInterpolate=1,
sourceAnnotations=0):
"""Render template in the provided template ``context``.
diff --git a/src/zope/pagetemplate/pagetemplate.py b/src/zope/pagetemplate/pagetemplate.py
index e254844..9491885 100644
--- a/src/zope/pagetemplate/pagetemplate.py
+++ b/src/zope/pagetemplate/pagetemplate.py
@@ -78,12 +78,13 @@ class PageTemplate(object):
expand = 1
_v_errors = ()
_v_cooked = 0
+ _v_macros = None
_v_program = None
_text = ''
def macros(self):
self._cook_check()
- return self._v_program.macros
+ return self._v_macros
macros = property(macros)
@@ -127,7 +128,7 @@ class PageTemplate(object):
context = self.pt_getEngineContext(namespace)
return self._v_program(
- context, tal=not source, showtal=showtal,
+ context, self._v_macros, tal=not source, showtal=showtal,
strictinsert=0, sourceAnnotations=sourceAnnotations
)
@@ -204,7 +205,7 @@ class PageTemplate(object):
engine = queryUtility(
IPageTemplateEngine, default=PageTemplateEngine
)
- self._v_program = engine.cook(
+ self._v_program, self._v_macros = engine.cook(
source_file, self._text, pt_engine, self.content_type)
except:
etype, e = sys.exc_info()[:2]
@@ -227,14 +228,13 @@ class PageTemplateEngine(object):
implements(IPageTemplateProgram)
classProvides(IPageTemplateEngine)
- def __init__(self, program, macros):
- self.macros = macros
- self._program = program
+ def __init__(self, program):
+ self.program = program
- def __call__(self, context, **options):
+ def __call__(self, context, macros, **options):
output = StringIO(u'')
interpreter = TALInterpreter(
- self._program, self.macros, context,
+ self.program, macros, context,
stream=output, **options
)
interpreter()
@@ -252,7 +252,7 @@ class PageTemplateEngine(object):
parser.parseString(text)
program, macros = parser.getCode()
- return cls(program, macros)
+ return cls(program), macros
class PageTemplateTracebackSupplement(object):
diff --git a/src/zope/pagetemplate/tests/test_basictemplate.py b/src/zope/pagetemplate/tests/test_basictemplate.py
index 02a1984..4f4d8c5 100644
--- a/src/zope/pagetemplate/tests/test_basictemplate.py
+++ b/src/zope/pagetemplate/tests/test_basictemplate.py
@@ -93,14 +93,19 @@ class BasicTemplateTests(unittest.TestCase):
return self.args, args, kwargs
class DummyEngine(object):
- cook = DummyProgram
+ @staticmethod
+ def cook(*args):
+ return DummyProgram(*args), "macros"
provideUtility(DummyEngine, IPageTemplateEngine)
self.t._cook()
+ self.assertTrue(isinstance(self.t._v_program, DummyProgram))
+ self.assertEqual(self.t._v_macros, "macros")
+
# "Render" and unpack arguments passed for verification
((cls, source_file, text, engine, content_type),
- (program, context),
+ (program, context, macros),
options) = \
self.t.pt_render({})