[Checkins] SVN: zope.browserpage/trunk/ Also moved named template implementation from zope.app.pagetemplate here.

Hanno Schlichting hannosch at hannosch.eu
Tue Dec 22 16:35:56 EST 2009


Log message for revision 106934:
  Also moved named template implementation from zope.app.pagetemplate here.
  

Changed:
  U   zope.browserpage/trunk/CHANGES.txt
  U   zope.browserpage/trunk/setup.py
  U   zope.browserpage/trunk/src/zope/browserpage/configure.zcml
  A   zope.browserpage/trunk/src/zope/browserpage/namedtemplate.py
  A   zope.browserpage/trunk/src/zope/browserpage/namedtemplate.txt
  A   zope.browserpage/trunk/src/zope/browserpage/tests/namedtemplate.pt
  A   zope.browserpage/trunk/src/zope/browserpage/tests/test_namedtemplate.py

-=-
Modified: zope.browserpage/trunk/CHANGES.txt
===================================================================
--- zope.browserpage/trunk/CHANGES.txt	2009-12-22 21:31:06 UTC (rev 106933)
+++ zope.browserpage/trunk/CHANGES.txt	2009-12-22 21:35:55 UTC (rev 106934)
@@ -2,9 +2,10 @@
 CHANGES
 =======
 
-3.10.2 (unreleased)
+3.11.0 (unreleased)
 ===================
 
+- Also moved named template implementation from zope.app.pagetemplate here.
 
 3.10.1 (2009-12-22)
 ===================

Modified: zope.browserpage/trunk/setup.py
===================================================================
--- zope.browserpage/trunk/setup.py	2009-12-22 21:31:06 UTC (rev 106933)
+++ zope.browserpage/trunk/setup.py	2009-12-22 21:35:55 UTC (rev 106934)
@@ -19,7 +19,7 @@
                     open('CHANGES.txt').read())
 
 setup(name='zope.browserpage',
-      version = '3.10.2dev',
+      version = '3.11.0dev',
       url='http://pypi.python.org/pypi/zope.browserpage/',
       author='Zope Corporation and Contributors',
       author_email='zope-dev at zope.org',
@@ -47,6 +47,7 @@
                         'zope.publisher>=3.8',
                         'zope.schema',
                         'zope.security [untrustedpython]',
+                        'zope.traversing',
                         ],
       extras_require={
           'menu': ['zope.browsermenu'],

Modified: zope.browserpage/trunk/src/zope/browserpage/configure.zcml
===================================================================
--- zope.browserpage/trunk/src/zope/browserpage/configure.zcml	2009-12-22 21:31:06 UTC (rev 106933)
+++ zope.browserpage/trunk/src/zope/browserpage/configure.zcml	2009-12-22 21:35:55 UTC (rev 106934)
@@ -11,4 +11,11 @@
      factory="zope.browserpage.viewpagetemplatefile.NoTraverser"
      />
 
+  <!-- TALES namespace allows view/template:default etc -->
+  <adapter
+      for="*"
+      factory=".namedtemplate.NamedTemplatePathAdapter"
+      name="template"
+      />
+
 </configure>

Copied: zope.browserpage/trunk/src/zope/browserpage/namedtemplate.py (from rev 106652, zope.app.pagetemplate/trunk/src/zope/app/pagetemplate/namedtemplate.py)
===================================================================
--- zope.browserpage/trunk/src/zope/browserpage/namedtemplate.py	                        (rev 0)
+++ zope.browserpage/trunk/src/zope/browserpage/namedtemplate.py	2009-12-22 21:35:55 UTC (rev 106934)
@@ -0,0 +1,75 @@
+##############################################################################
+#
+# Copyright (c) 2005-2009 Zope Corporation 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.
+#
+##############################################################################
+"""
+"""
+
+from zope import component, interface
+import zope.traversing.interfaces
+
+class INamedTemplate(interface.Interface):
+    """A template that is looked up by name
+    """
+
+class NamedTemplateImplementation:
+
+    def __init__(self, descriptor, view_type=None):
+        try:
+            descriptor.__get__
+        except AttributeError:
+            raise TypeError(
+                "NamedTemplateImplementation must be passed a descriptor."
+                )
+        self.descriptor = descriptor
+        interface.implementer(INamedTemplate)(self)
+
+        if view_type is not None:
+            component.adapter(view_type)(self)
+
+    def __call__(self, instance):
+        return self.descriptor.__get__(instance, instance.__class__)
+
+
+class implementation:
+
+    def __init__(self, view_type=None):
+        self.view_type = view_type
+
+    def __call__(self, descriptor):
+        return NamedTemplateImplementation(descriptor, self.view_type)
+
+
+class NamedTemplate(object):
+
+    def __init__(self, name):
+        self.__name__ = name
+
+    def __get__(self, instance, type=None):
+        if instance is None:
+            return self
+        return component.getAdapter(instance, INamedTemplate, self.__name__)
+
+    def __call__(self, instance, *args, **kw):
+        self.__get__(instance)(*args, **kw)
+
+
+# TODO need test
+class NamedTemplatePathAdapter(object):
+
+    interface.implements(zope.traversing.interfaces.IPathAdapter)
+
+    def __init__(self, context):
+        self.context = context
+
+    def __getitem__(self, name):
+        return component.getAdapter(self.context, INamedTemplate, name)

Copied: zope.browserpage/trunk/src/zope/browserpage/namedtemplate.txt (from rev 106892, zope.app.pagetemplate/trunk/src/zope/app/pagetemplate/namedtemplate.txt)
===================================================================
--- zope.browserpage/trunk/src/zope/browserpage/namedtemplate.txt	                        (rev 0)
+++ zope.browserpage/trunk/src/zope/browserpage/namedtemplate.txt	2009-12-22 21:35:55 UTC (rev 106934)
@@ -0,0 +1,73 @@
+===============
+Named Templates
+===============
+
+We often want to be able to define view logic and view templates
+independently.  We'd like to be able to change the template used by a
+form without being forced to modify the form.
+
+Named templates provide templates that are registered as named view
+adapters.   To define a named template, use the `NamedTemplateImplementation`
+constructor:
+
+    >>> from zope.browserpage import ViewPageTemplateFile
+    >>> from zope.browserpage.namedtemplate import (
+    ...     NamedTemplateImplementation)
+    >>> sample = ViewPageTemplateFile('tests/namedtemplate.pt')
+    >>> sample = NamedTemplateImplementation(sample)
+
+Let's define a view that uses the named template.  To use a named
+template, use the NamedTemplate constructor, and give a template name:
+
+    >>> from zope.browserpage.namedtemplate import NamedTemplate
+    >>> class MyView:
+    ...     def __init__(self, context, request):
+    ...         self.context = context
+    ...         self.request = request
+    ...
+    ...     __call__ = NamedTemplate('sample')
+
+Normally, we'd register a named template for a view interface, to
+allow it to be registered for multiple views.  We'll just register it
+for our view class.
+
+    >>> from zope import component
+    >>> component.provideAdapter(sample, [MyView], name='sample')
+
+Now, with this in place, we should be able to use our view:
+
+    >>> class MyContent:
+    ...     def __init__(self, name):
+    ...         self.name = name
+
+    >>> from zope.publisher.browser import TestRequest
+    >>> print MyView(MyContent('bob'), TestRequest())(x=42)
+    <html><body>
+    Hello bob
+    The URL is http://127.0.0.1
+    The positional arguments were ()
+    The keyword argument x is 42
+    </body></html>
+    <BLANKLINE>
+
+The view type that a named template is to be used for can be supplied
+when the named template is created:
+
+    >>> class MyView:
+    ...     def __init__(self, context, request):
+    ...         self.context = context
+    ...         self.request = request
+    ...
+    ...     __call__ = NamedTemplate('sample2')
+
+    >>> sample = ViewPageTemplateFile('tests/namedtemplate.pt')
+    >>> sample = NamedTemplateImplementation(sample, MyView)
+    >>> component.provideAdapter(sample, name='sample2')
+    >>> print MyView(MyContent('bob'), TestRequest())(x=42)
+    <html><body>
+    Hello bob
+    The URL is http://127.0.0.1
+    The positional arguments were ()
+    The keyword argument x is 42
+    </body></html>
+    <BLANKLINE>

Copied: zope.browserpage/trunk/src/zope/browserpage/tests/namedtemplate.pt (from rev 106889, zope.app.pagetemplate/trunk/src/zope/app/pagetemplate/tests/namedtemplate.pt)
===================================================================
--- zope.browserpage/trunk/src/zope/browserpage/tests/namedtemplate.pt	                        (rev 0)
+++ zope.browserpage/trunk/src/zope/browserpage/tests/namedtemplate.pt	2009-12-22 21:35:55 UTC (rev 106934)
@@ -0,0 +1,6 @@
+<html><body>
+Hello <span tal:replace="context/name" />
+The URL is <span tal:replace="request/URL" />
+The positional arguments were <span tal:replace="args" />
+The keyword argument x is <span tal:replace="options/x" />
+</body></html>

Copied: zope.browserpage/trunk/src/zope/browserpage/tests/test_namedtemplate.py (from rev 106889, zope.app.pagetemplate/trunk/src/zope/app/pagetemplate/tests/test_namedtemplate.py)
===================================================================
--- zope.browserpage/trunk/src/zope/browserpage/tests/test_namedtemplate.py	                        (rev 0)
+++ zope.browserpage/trunk/src/zope/browserpage/tests/test_namedtemplate.py	2009-12-22 21:35:55 UTC (rev 106934)
@@ -0,0 +1,37 @@
+##############################################################################
+#
+# Copyright (c) 2005-2009 Zope Corporation 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$
+"""
+import os
+import os.path
+import zope.component.testing
+import zope.traversing.adapters
+
+
+def pageSetUp(test):
+    zope.component.testing.setUp(test)
+    zope.component.provideAdapter(
+        zope.traversing.adapters.DefaultTraversable,
+        [None],
+        )
+
+
+def test_suite():
+    from zope.testing import doctest
+    return doctest.DocFileSuite(
+        os.path.join(os.pardir, 'namedtemplate.txt'),
+        setUp=pageSetUp, tearDown=zope.component.testing.tearDown,
+        )



More information about the checkins mailing list