[Checkins] SVN: z3c.viewtemplate/trunk/src/z3c/viewtemplate/ Allow teh selection of a single macro from the template.

Jürgen Kartnaller juergen at kartnaller.at
Mon Sep 18 10:06:51 EDT 2006


Log message for revision 70217:
  Allow teh selection of a single macro from the template.
  

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

-=-
Modified: z3c.viewtemplate/trunk/src/z3c/viewtemplate/README.txt
===================================================================
--- z3c.viewtemplate/trunk/src/z3c/viewtemplate/README.txt	2006-09-18 07:20:01 UTC (rev 70216)
+++ z3c.viewtemplate/trunk/src/z3c/viewtemplate/README.txt	2006-09-18 14:06:50 UTC (rev 70217)
@@ -59,7 +59,7 @@
 
 The template factory allows us to create a ViewPageTeplateFile instance.
 
-  >>> factory = TemplateFactory(template, 'text/html')
+  >>> factory = TemplateFactory(template, None, 'text/html')
 
 We register the factory on a view interface and a layer.
 
@@ -81,7 +81,7 @@
 
   >>> myTemplate = os.path.join(temp_dir, 'demoTemplate.pt')
   >>> open(myTemplate, 'w').write('''<div>IMyView</div>''')
-  >>> factory = TemplateFactory(myTemplate, 'text/html')
+  >>> factory = TemplateFactory(myTemplate, None, 'text/html')
   >>> component.provideAdapter(factory,
   ...            (IMyView, IDefaultBrowserLayer),
   ...            IPageTemplate)
@@ -107,7 +107,23 @@
   >>> print templatedView()
   <div>view</div>
 
+Use of macros.
 
+  >>> macroTemplate = os.path.join(temp_dir, 'macroTemplate.pt')
+  >>> open(macroTemplate, 'w').write('''
+  ...   <metal:block define-macro="macro1">
+  ...     <div>macro1</div>
+  ...   </metal:block>
+  ...   <metal:block define-macro="macro2">
+  ...     <div>macro2</div>
+  ...   </metal:block>
+  ...   ''')
+
+  >>> factory = TemplateFactory(macroTemplate, 'macro1', 'text/html')
+  >>> print factory(view, request)()
+  <div>macro1</div>
+
+
 Why didn't we use named templates from the ``zope.formlib`` package?
 
 While named templates allow us to separate the view code from the template

Added: z3c.viewtemplate/trunk/src/z3c/viewtemplate/macro.py
===================================================================
--- z3c.viewtemplate/trunk/src/z3c/viewtemplate/macro.py	2006-09-18 07:20:01 UTC (rev 70216)
+++ z3c.viewtemplate/trunk/src/z3c/viewtemplate/macro.py	2006-09-18 14:06:50 UTC (rev 70217)
@@ -0,0 +1,41 @@
+##############################################################################
+#
+# 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 StringIO import StringIO
+
+from zope.tal.talinterpreter import TALInterpreter
+
+class Macro(object):
+    """Provides a single macro from a template for rendering."""
+
+    def __init__(self, template, macroName, view, request):
+        self.template = template
+        self.macroName = macroName
+        self.view = view
+        self.request = request
+
+    def __call__(self, *args, **kwargs):
+        program = self.template.macros[self.macroName]
+        output = StringIO(u'')
+        namespace = self.template.pt_getContext(self.view, self.request)
+        context = self.template.pt_getEngineContext(namespace)
+        TALInterpreter(program, None,
+                       context, output, tal=True, showtal=False,
+                       strictinsert=0, sourceAnnotations=False)()
+        return output.getvalue()
+


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

Modified: z3c.viewtemplate/trunk/src/z3c/viewtemplate/zcml.py
===================================================================
--- z3c.viewtemplate/trunk/src/z3c/viewtemplate/zcml.py	2006-09-18 07:20:01 UTC (rev 70216)
+++ z3c.viewtemplate/trunk/src/z3c/viewtemplate/zcml.py	2006-09-18 14:06:50 UTC (rev 70217)
@@ -33,6 +33,8 @@
 
 from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
 
+from z3c.viewtemplate.macro import Macro
+
 from zope.i18nmessageid import MessageFactory
 _ = MessageFactory('zope')
 
@@ -47,6 +49,20 @@
             required=False,
             )
 
+    macro = schema.TextLine(
+            title = _(u'Macro'),
+            description = _(u"""
+                The macro to be used.
+                This allows us to define different macros in on template.
+                The template designer can now create hole site, the
+                ViewTemplate can then extract the macros for single viewlets
+                or views.
+                If no macro is given the hole template is used for rendering.
+                """),
+            required = False,
+            default = u'',
+            )
+
     for_ = GlobalObject(
             title = _(u'View'),
             description = _(u'The view for which the template should be used'),
@@ -72,17 +88,22 @@
 
 class TemplateFactory(object):
 
-    def __init__(self, filename, contentType):
+    def __init__(self, filename, macro, contentType):
         self.filename = filename
+        self.macro = macro
         self.contentType = contentType
 
     def __call__(self, view, request):
-        return ViewPageTemplateFile(self.filename,
-                                    content_type=self.contentType)
+        template = ViewPageTemplateFile(self.filename,
+                                        content_type=self.contentType)
+        if self.macro is None:
+            return template
+        return Macro(template, self.macro, view, request)
 
 
 def templateDirective(_context,
                       template,
+                      macro=None,
                       for_=interface.Interface,
                       layer=IDefaultBrowserLayer,
                       contentType='text/html',
@@ -92,7 +113,7 @@
     if not os.path.isfile(template):
         raise ConfigurationError("No such file", template)
 
-    factory = TemplateFactory(template, contentType)
+    factory = TemplateFactory(template, macro, contentType)
 
     # register the template
     zcml.adapter(_context, (factory,), IPageTemplate, (for_, layer))



More information about the Checkins mailing list