[Zope-CVS] CVS: Products/CompositePage - composite.py:1.7 macro.py:1.3

Shane Hathaway cvs-admin at zope.org
Wed Nov 5 15:05:21 EST 2003


Update of /cvs-repository/Products/CompositePage
In directory cvs.zope.org:/tmp/cvs-serv24456

Modified Files:
	composite.py macro.py 
Log Message:
Refactored to allow templates to use renderMacro().

It may make more sense for templates to choose which macro to render
rather than for composites to make that choice.  This refactoring makes
that possible.


=== Products/CompositePage/composite.py 1.6 => 1.7 ===
--- Products/CompositePage/composite.py:1.6	Tue Oct 14 17:42:52 2003
+++ Products/CompositePage/composite.py	Wed Nov  5 15:04:50 2003
@@ -104,13 +104,6 @@
     def getTemplate(self):
         return self.restrictedTraverse(str(self.template_path))
 
-    security.declareProtected(perm_names.view, "getMacro")
-    def getMacro(self):
-        if not self.use_macro:
-            return None
-        template = self.getTemplate()
-        return template.restrictedTraverse(("macros", str(self.use_macro)))
-
     security.declareProtected(perm_names.change_composites, "generateSlots")
     def generateSlots(self):
         """Creates the slots defined by the template.
@@ -129,11 +122,12 @@
             raise CompositeError("Circular composite reference")
         self._v_rendering = 1
         try:
-            macro = self.getMacro()
-            if macro is not None:
-                return renderMacro(macro, self)
+            template = self.getTemplate()
+            if self.use_macro:
+                return renderMacro(template, self.use_macro, self)
+            elif hasattr(template, "_render_composite"):
+                return template._render_composite(self)
             else:
-                template = self.getTemplate()
                 return template()
         finally:
             self._v_rendering = 0


=== Products/CompositePage/macro.py 1.2 => 1.3 ===
--- Products/CompositePage/macro.py:1.2	Sat Oct  4 14:20:22 2003
+++ Products/CompositePage/macro.py	Wed Nov  5 15:04:50 2003
@@ -22,6 +22,7 @@
 
 from Acquisition import aq_inner, aq_parent
 from AccessControl import getSecurityManager
+from AccessControl.ZopeGuards import guarded_getattr
 from TAL.TALInterpreter import TALInterpreter
 from Products.PageTemplates.Expressions import getEngine, SecureModuleImporter
 
@@ -29,7 +30,7 @@
 
 
 class MacroSlotGenerator:
-    """Generates macro slots on the fly.
+    """Renders macro slots on the fly.
     """
 
     def __init__(self, slots):
@@ -42,18 +43,24 @@
         return [('rawtextOffset', (text, 0)),]
 
 
-def makeContext(composite):
+def makeContext(template, composite=None, options=None):
     """Creates a TALES context.
     """
-    root = composite.getPhysicalRoot()
-    parent = aq_parent(composite)
+    root = template.getPhysicalRoot()
+    if composite is not None:
+        parent = composite
+    else:
+        parent = aq_parent(template)
+    if options is None:
+        options = {}
     c = {
-        'template': composite,  # Not a mistake :-)
+        'composite': composite,
+        'template': template,
         'here': parent,
         'context': parent,
-        'container': aq_parent(aq_inner(composite)),
+        'container': aq_parent(aq_inner(template)),
         'nothing': None,
-        'options': {},
+        'options': options,
         'root': root,
         'user': getSecurityManager().getUser(),
         'request': getattr(root, 'REQUEST', None),
@@ -62,12 +69,18 @@
     return getEngine().getContext(c)
     
 
-def renderMacro(macro, composite):
+def renderMacro(template, macro_name, composite=None, options=None):
     """Renders a page template macro, filling slots.
     """
-    path = '/'.join(composite.getPhysicalPath())
+    macro = guarded_getattr(template, "macros")[macro_name]
+    if composite is not None:
+        path = '/'.join(composite.getPhysicalPath())
+        slots = guarded_getattr(composite, "slots")
+        slot_generator = MacroSlotGenerator(slots)
+    else:
+        path = '/'.join(template.getPhysicalPath())
+        slot_generator = {}
     expr = lambda engine: macro
-    slot_generator = MacroSlotGenerator(composite.slots)
     program = [
         ('version', '1.4'),
         ('mode', 'html'),
@@ -76,8 +89,7 @@
         ]
 
     output = StringIO()
-    c = makeContext(composite)
+    c = makeContext(template, composite, options)
     TALInterpreter(program, {}, c, output, tal=1, strictinsert=0)()
     return output.getvalue()
     
-




More information about the Zope-CVS mailing list