[Zope-Checkins] CVS: Zope3/lib/python/Zope/PageTemplate - PageTemplate.py:1.1.2.10
Fred L. Drake, Jr.
fdrake@acm.org
Fri, 1 Feb 2002 17:09:21 -0500
Update of /cvs-repository/Zope3/lib/python/Zope/PageTemplate
In directory cvs.zope.org:/tmp/cvs-serv2820
Modified Files:
Tag: Zope-3x-branch
PageTemplate.py
Log Message:
Simplify the PageTemplate base class, restoring the ability to use it outside
of the Zope application server.
Added some information on how to subclass to the class docstring.
Moved some of the helper classes to other modules, where they are actually
needed (for Zope-specific behavior.
=== Zope3/lib/python/Zope/PageTemplate/PageTemplate.py 1.1.2.9 => 1.1.2.10 ===
from Zope.TAL.TALGenerator import TALGenerator
from Zope.TAL.TALInterpreter import TALInterpreter
-from Zope.ComponentArchitecture import getRequestView
from Expressions import getEngine
from string import join, strip, rstrip, split, replace, lower, find
from cStringIO import StringIO
@@ -33,7 +32,29 @@
return parent._v_macros
class PageTemplate(object):
- "Page Templates using TAL, TALES, and METAL"
+ """Page Templates using TAL, TALES, and METAL.
+
+ Subclassing
+ -----------
+
+ The following methods have certain internal responsibilities.
+
+ pt_getContext(**keywords)
+ Should ignore keyword arguments that it doesn't care about,
+ and construct the namespace passed to the TALES expression
+ engine. This method is free to use the keyword arguments it
+ receives.
+
+ pt_render(namespace, source=0)
+ Responsible the TAL interpreter to perform the rendering. The
+ namespace argument is a mapping which defines the top-level
+ namespaces passed to the TALES expression engine.
+
+ __call__(*args, **keywords)
+ Calls pt_getContext() to construct the top-level namespace
+ passed to the TALES expression engine, then calls pt_render()
+ to perform the rendering.
+ """
content_type = 'text/html'
expand = 1
@@ -51,21 +72,15 @@
text = text.read()
self.write(text)
- def pt_getContext(self):
- c = {'template': self,
- 'options': {},
- 'nothing': None,
- 'modules': ModuleImporter,
- }
-## if inst is not None:
-## c['views'] = ViewMapper(inst.getContext())
-## c['here'] = inst.getContext()
-## c['container'] = inst
- # XXX this needs to be changed when we have context
- c['root'] = None
- return c
+ def pt_getContext(self, args=(), options={}, **ignored):
+ return {'template': self,
+ 'options': options,
+ 'args': args,
+ 'nothing': None,
+ 'modules': ModuleImporter,
+ }
- def pt_render(self, source=0, extra_context={}):
+ def pt_render(self, namespace, source=0):
"""Render this Page Template"""
if self._v_errors:
raise PTRuntimeError(
@@ -74,26 +89,17 @@
''' % ('', #XXX self.id
self._v_errors))
output = StringIO()
- c = self.pt_getContext()
- c.update(extra_context)
if Z_DEBUG_MODE:
- __traceback_info__ = pprint.pformat(c)
+ __traceback_info__ = pprint.pformat(namespace)
TALInterpreter(self._v_program, self._v_macros,
- getEngine().getContext(c),
+ getEngine().getContext(namespace),
output,
tal=not source, strictinsert=0)()
return output.getvalue()
- def __call__(self, inst=None, *args, **kwargs):
- if not kwargs.has_key('args'):
- kwargs['args'] = args
- extra_context = {'options': kwargs}
- if inst is not None:
- extra_context['here'] = inst.getContext()
- extra_context['views'] = ViewMapper(inst.getContext(), None)
- extra_context['container'] = inst
- return self.pt_render(extra_context=extra_context)
+ def __call__(self, *args, **kwargs):
+ return self.pt_render(self.pt_getContext(*args, **kwargs))
def pt_errors(self):
err = self._v_errors
@@ -159,36 +165,6 @@
if not hasattr(getattr(self, 'aq_base', self), 'is_html'):
return self.content_type == 'text/html'
return self.is_html
-
- def __get__(self, instance, type=None):
- return BoundPageTemplate(self, instance)
-
-
-class BoundPageTemplate(object):
- def __init__(self, pt, ob):
- object.__setattr__(self, 'im_func', pt)
- object.__setattr__(self, 'im_self', ob)
-
- def __call__(self, REQUEST=None, **kw):
- return self.im_func(self.im_self, REQUEST=REQUEST, **kw)
-
- def __getattr__(self, name):
- return getattr(self.im_func, name)
-
- def __setattr__(self, name, v):
- raise AttributeError("Can't set attribute", name)
-
- def __repr__(self):
- return "<BoundPageTemplateFile of %r>" % self.im_self
-
-
-class ViewMapper:
- def __init__(self, ob, request):
- self.ob = ob
- self.request = request
-
- def __getitem__(self, name):
- return getRequestView(self.ob, name, self.request)
class _ModuleImporter: