[Checkins] SVN: z3c.viewtemplate/trunk/src/z3c/viewtemplate/ Added an implementation to use registered templates as 'normal' PageTemplate.

Jürgen Kartnaller juergen at kartnaller.at
Sun Oct 8 12:24:55 EDT 2006


Log message for revision 70577:
  Added an implementation to use registered templates as 'normal' PageTemplate.
  
  The RegisteredPageTemplate allows us to use the new template registration
  system with all existing implementations such as `zope.formlib` and
  `zope.viewlet`.
  

Changed:
  U   z3c.viewtemplate/trunk/src/z3c/viewtemplate/README.txt
  A   z3c.viewtemplate/trunk/src/z3c/viewtemplate/pagetemplate.py

-=-
Modified: z3c.viewtemplate/trunk/src/z3c/viewtemplate/README.txt
===================================================================
--- z3c.viewtemplate/trunk/src/z3c/viewtemplate/README.txt	2006-10-08 14:10:11 UTC (rev 70576)
+++ z3c.viewtemplate/trunk/src/z3c/viewtemplate/README.txt	2006-10-08 16:24:54 UTC (rev 70577)
@@ -79,7 +79,7 @@
 
 Now we register a new template on the specific interface of our view.
 
-  >>> myTemplate = os.path.join(temp_dir, 'demoTemplate.pt')
+  >>> myTemplate = os.path.join(temp_dir, 'myViewTemplate.pt')
   >>> open(myTemplate, 'w').write('''<div>IMyView</div>''')
   >>> factory = TemplateFactory(myTemplate, None, 'text/html')
   >>> component.provideAdapter(factory,
@@ -130,3 +130,36 @@
 registration, they are not registerable for a particular layer making it
 impossible to implement multiple skins using named templates.
 
+
+Page Template
+-------------
+
+And for the simplest possible use we provide a RegisteredPageTemplate a la
+ViewPageTemplateFile or NamedTemplate.
+
+The RegisteredPageTemplate allows us to use new template registration system
+with all existing implementations such as `zope.formlib` and `zope.viewlet`.
+
+  >>> from z3c.viewtemplate.pagetemplate import RegisteredPageTemplate
+  >>> class IMyUseOfView(interface.Interface):
+  ...     pass
+  >>> class UseOfRegisteredPageTemplate(object):
+  ...     interface.implements(IMyUseOfView)
+  ...
+  ...     template = RegisteredPageTemplate()
+  ...
+  ...     def __init__(self, context, request):
+  ...         self.context = context
+  ...         self.request = request
+
+  >>> simple = UseOfRegisteredPageTemplate(root, request)
+  >>> print simple.template()
+  <div>demo</div>
+
+  >>> factory = TemplateFactory(macroTemplate, 'macro2', 'text/html')
+  >>> component.provideAdapter(factory,
+  ...            (IMyUseOfView, IDefaultBrowserLayer),
+  ...            IPageTemplate)
+  >>> print simple.template()
+  <div>macro2</div>
+

Added: z3c.viewtemplate/trunk/src/z3c/viewtemplate/pagetemplate.py
===================================================================
--- z3c.viewtemplate/trunk/src/z3c/viewtemplate/pagetemplate.py	2006-10-08 14:10:11 UTC (rev 70576)
+++ z3c.viewtemplate/trunk/src/z3c/viewtemplate/pagetemplate.py	2006-10-08 16:24:54 UTC (rev 70577)
@@ -0,0 +1,58 @@
+##############################################################################
+#
+# Copyright (c) 2005 Zope Foundation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+from zope import interface
+from zope import component
+
+from zope.pagetemplate.interfaces import IPageTemplate
+from zope.publisher.browser import BrowserView
+
+
+class RegisteredPageTemplate(object):
+
+    def __init__(self, content_type=None):
+        if content_type is not None:
+            self.content_type = content_type
+
+    def __call__(self, instance):
+        template = component.getMultiAdapter(
+                (instance, instance.request), IPageTemplate)
+        return template(instance)
+
+    def __get__(self, instance, type):
+        return BoundRegisteredPageTemplate(self, instance)
+
+
+class BoundRegisteredPageTemplate(object):
+    def __init__(self, pt, ob):
+        object.__setattr__(self, 'im_func', pt)
+        object.__setattr__(self, 'im_self', ob)
+
+    def __call__(self, *args, **kw):
+        if self.im_self is None:
+            im_self, args = args[0], args[1:]
+        else:
+            im_self = self.im_self
+        return self.im_func(im_self, *args, **kw)
+
+    def __setattr__(self, name, v):
+        raise AttributeError("Can't set attribute", name)
+
+    def __repr__(self):
+        return "<BoundRegisteredPageTemplateFile of %r>" % self.im_self
+


Property changes on: z3c.viewtemplate/trunk/src/z3c/viewtemplate/pagetemplate.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Checkins mailing list