[Zope-Checkins] CVS: Zope3/lib/python/Zope/PageTemplate - ZPT.py:1.1.2.1 ViewZPT.py:1.1.2.1

Fred L. Drake, Jr. fdrake@acm.org
Fri, 1 Feb 2002 17:10:48 -0500


Update of /cvs-repository/Zope3/lib/python/Zope/PageTemplate
In directory cvs.zope.org:/tmp/cvs-serv2898

Added Files:
      Tag: Zope-3x-branch
	ZPT.py ViewZPT.py 
Log Message:
Add Zope-specific specializations of PageTemplate.  This avoids mingling
Zope-only and Zope-agnostic code in the PageTemplate base class.


=== Added File Zope3/lib/python/Zope/PageTemplate/ZPT.py ===
"""
"""

from Zope.ContextWrapper.wrapper import getinnerwrapper
from Zope.PageTemplate.PageTemplate import PageTemplate


class ZPT(PageTemplate):
    """ """
    def pt_getContext(self, instance, request, **keywords):
        namespace = PageTemplate.pt_getContext(self, **keywords)
        namespace['container'] = getinnerwrapper(instance)
        namespace['here'] = instance
        namespace['request'] = request
        return namespace

    def __call__(self, instance, request, **keywords):
        namespace = self.pt_getContext(request=request, instance=instance,
                                       options=keywords)
        return self.pt_render(namespace)

    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, **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



=== Added File Zope3/lib/python/Zope/PageTemplate/ViewZPT.py ===
"""
PageTemplate that can be used as part of a View.  This adds the
'views' top-level name.
"""

from Zope.ComponentArchitecture import getRequestView
from Zope.PageTemplate.ZPT import ZPT


class ViewZPT(ZPT):
    def pt_getContext(self, instance, **keywords):
        # instance is a View component
        namespace = ZPT.pt_getContext(self, instance=instance, **keywords)
        namespace['here'] = instance.getContext()
        namespace['views'] = ViewMapper(namespace['here'],
                                        namespace['request'])
        return namespace


class ViewMapper:
    def __init__(self, ob, request):
        self.ob = ob
        self.request = request 

    def __getitem__(self, name):
        return getRequestView(self.ob, name, self.request)