[Zope3-checkins] SVN: Zope3/trunk/src/zope/viewlet/ Added base classes for CSS and JavaScript bundle viewlets.

Roger Ineichen roger at projekt01.ch
Sat Jan 27 12:16:07 EST 2007


Log message for revision 72238:
  Added base classes for CSS and JavaScript bundle viewlets.
  Added tests for CSS and JavaScript bundle viewlets.

Changed:
  U   Zope3/trunk/src/zope/viewlet/README.txt
  A   Zope3/trunk/src/zope/viewlet/css_bundle_viewlet.pt
  A   Zope3/trunk/src/zope/viewlet/javascript_bundle_viewlet.pt
  U   Zope3/trunk/src/zope/viewlet/viewlet.py

-=-
Modified: Zope3/trunk/src/zope/viewlet/README.txt
===================================================================
--- Zope3/trunk/src/zope/viewlet/README.txt	2007-01-27 09:34:51 UTC (rev 72237)
+++ Zope3/trunk/src/zope/viewlet/README.txt	2007-01-27 17:16:05 UTC (rev 72238)
@@ -373,6 +373,31 @@
   <script type="text/javascript" src="/@@/resource.js">
   </script>
 
+
+There is also a javascript viewlet base class which knows how to render more 
+then one javascript resource file:
+
+  >>> class JSSecondResource(object):
+  ...     def __init__(self, request):
+  ...         self.request = request
+  ...
+  ...     def __call__(self):
+  ...         return '/@@/second-resource.js'
+
+  >>> zope.component.provideAdapter(
+  ...     JSSecondResource,
+  ...     (IDefaultBrowserLayer,),
+  ...     zope.interface.Interface, name='second-resource.js')
+
+  >>> JSBundleViewlet = viewlet.JavaScriptBundleViewlet(('resource.js',
+  ...                                                    'second-resource.js'))
+  >>> print JSBundleViewlet(content, request, view, manager).render().strip()
+  <script type="text/javascript"
+          src="/@@/resource.js"> </script>
+  <script type="text/javascript"
+          src="/@@/second-resource.js"> </script>
+
+
 The same works for the CSS resource viewlet:
 
   >>> class CSSResource(object):
@@ -399,7 +424,31 @@
   <link type="text/css" rel="css" href="/@@/resource.css"
         media="print" />
 
+There is also a bundle viewlet for CSS links:
 
+  >>> class CSSPrintResource(object):
+  ...     def __init__(self, request):
+  ...         self.request = request
+  ...
+  ...     def __call__(self):
+  ...         return '/@@/print-resource.css'
+
+  >>> zope.component.provideAdapter(
+  ...     CSSPrintResource,
+  ...     (IDefaultBrowserLayer,),
+  ...     zope.interface.Interface, name='print-resource.css')
+
+  >>> items = []
+  >>> items.append({'path':'resource.css', 'rel':'stylesheet', 'media':'all'})
+  >>> items.append({'path':'print-resource.css', 'media':'print'})
+  >>> CSSBundleViewlet = viewlet.CSSBundleViewlet(items)
+  >>> print CSSBundleViewlet(content, request, view, manager).render().strip()
+  <link type="text/css" rel="stylesheet"
+        href="/@@/resource.css" media="all" />
+  <link type="text/css" rel="stylesheet"
+        href="/@@/print-resource.css" media="print" />
+
+
 A Complex Example
 -----------------
 

Added: Zope3/trunk/src/zope/viewlet/css_bundle_viewlet.pt
===================================================================
--- Zope3/trunk/src/zope/viewlet/css_bundle_viewlet.pt	2007-01-27 09:34:51 UTC (rev 72237)
+++ Zope3/trunk/src/zope/viewlet/css_bundle_viewlet.pt	2007-01-27 17:16:05 UTC (rev 72238)
@@ -0,0 +1,6 @@
+<tal:block repeat="info view/getResources">
+<link type="text/css" rel="stylesheet" href="somestyle.css" media="all"
+      tal:attributes="rel info/rel;
+                      href info/url;
+                      media info/media" />
+</tal:block>


Property changes on: Zope3/trunk/src/zope/viewlet/css_bundle_viewlet.pt
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Zope3/trunk/src/zope/viewlet/javascript_bundle_viewlet.pt
===================================================================
--- Zope3/trunk/src/zope/viewlet/javascript_bundle_viewlet.pt	2007-01-27 09:34:51 UTC (rev 72237)
+++ Zope3/trunk/src/zope/viewlet/javascript_bundle_viewlet.pt	2007-01-27 17:16:05 UTC (rev 72238)
@@ -0,0 +1,4 @@
+<tal:block repeat="resource view/getResources">
+<script type="text/javascript" src="some-library.js"
+        tal:attributes="src resource"> </script>
+</tal:block>


Property changes on: Zope3/trunk/src/zope/viewlet/javascript_bundle_viewlet.pt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: Zope3/trunk/src/zope/viewlet/viewlet.py
===================================================================
--- Zope3/trunk/src/zope/viewlet/viewlet.py	2007-01-27 09:34:51 UTC (rev 72237)
+++ Zope3/trunk/src/zope/viewlet/viewlet.py	2007-01-27 17:16:05 UTC (rev 72238)
@@ -13,7 +13,7 @@
 ##############################################################################
 """Viewlet implementation
 
-$Id: metaconfigure.py 38437 2005-09-10 01:59:07Z rogerineichen $
+$Id$
 """
 __docformat__ = 'restructuredtext'
 
@@ -144,3 +144,79 @@
 
     return klass
 
+
+class ResourceBundleViewletBase(object):
+    """A simple viewlet for inserting references to different resources.
+
+    This is an abstract class that is expected to be used as a base only.
+    """
+    _paths = None
+
+    def getResources(self):
+        resources = []
+        append = resources.append
+        for path in self._paths:
+            append(api.traverse(self.context, '++resource++' + path,
+                request=self.request))
+        return resources
+
+    def render(self, *args, **kw):
+        return self.index(*args, **kw)
+
+
+def JavaScriptBundleViewlet(paths):
+    """Create a viewlet that can simply insert javascript links."""
+    src = os.path.join(os.path.dirname(__file__), 
+        'javascript_bundle_viewlet.pt')
+
+    klass = type('JavaScriptBundleViewlet',
+                 (ResourceBundleViewletBase, ViewletBase),
+                  {'index': ViewPageTemplateFile(src),
+                   '_paths': paths})
+
+    return klass
+
+
+class CSSResourceBundleViewletBase(object):
+    """A simple viewlet for inserting css references to different resources.
+
+    There is a tuple or list of dict used for the different resource 
+    descriptions. The list of dict uses the following format:
+    
+    ({path:'the path', media:'all', rel:'stylesheet'},...)
+    
+    The default values for media is ``all`` and the default value for rel is
+    ``stylesheet``. The path must be set there is no default value for the 
+    path attribute.
+    
+    This is an abstract class that is expected to be used as a base only.
+    """
+    _items = None
+
+    def getResources(self):
+        resources = []
+        append = resources.append
+        for item in self._items:
+            info = {}
+            info['url'] = api.traverse(self.context, 
+                '++resource++' + item.get('path'), request=self.request)
+            info['media'] = item.get('media', 'all')
+            info['rel'] = item.get('rel', 'stylesheet')
+            append(info)
+        return resources
+
+    def render(self, *args, **kw):
+        return self.index(*args, **kw)
+
+
+def CSSBundleViewlet(items):
+    """Create a viewlet that can simply insert css links."""
+    src = os.path.join(os.path.dirname(__file__), 
+        'css_bundle_viewlet.pt')
+
+    klass = type('CSSBundleViewlet',
+                 (CSSResourceBundleViewletBase, ViewletBase),
+                  {'index': ViewPageTemplateFile(src),
+                   '_items': items})
+
+    return klass



More information about the Zope3-Checkins mailing list