[Zope-CVS] CVS: Products/CompositePage - composite.py:1.9
macro.py:1.5 render.py:1.4
Shane Hathaway
shane at zope.com
Wed Dec 31 12:32:15 EST 2003
Update of /cvs-repository/Products/CompositePage
In directory cvs.zope.org:/tmp/cvs-serv16648
Modified Files:
composite.py macro.py render.py
Log Message:
CompositePage now detects METAL-based slotting automatically.
If the root element of the page template has a metal:define-macro directive,
the composite uses METAL slotting.
=== Products/CompositePage/composite.py 1.8 => 1.9 ===
--- Products/CompositePage/composite.py:1.8 Thu Dec 11 10:56:07 2003
+++ Products/CompositePage/composite.py Wed Dec 31 12:32:14 2003
@@ -29,7 +29,7 @@
from interfaces import ISlot, CompositeError
from slot import Slot
-from macro import renderMacro
+from macro import renderMacro, getRootMacro
import perm_names
_www = os.path.join(os.path.dirname(__file__), "www")
@@ -74,7 +74,6 @@
)
template_path = "template"
- use_macro = ""
_v_editing = 0
_v_rendering = 0
_v_generating = 0
@@ -85,8 +84,6 @@
_properties = Folder._properties + (
{"id": "template_path", "mode": "w", "type": "string",
"label": "Path to template"},
- {"id": "use_macro", "mode": "w", "type": "string",
- "label": "METAL macro to use from template"},
)
def __init__(self):
@@ -125,11 +122,13 @@
self._v_rendering = 1
try:
template = self.getTemplate()
- if self.use_macro:
- return renderMacro(template, self.use_macro, self)
- elif hasattr(template, "_render_composite"):
- return template._render_composite(self)
+ macro = getRootMacro(template)
+ if macro:
+ # Since the template defines a macro at the root, use
+ # METAL-based slotting.
+ return renderMacro(template, macro, self)
else:
+ # Otherwise use TAL-based slotting.
return template()
finally:
self._v_rendering = 0
@@ -198,7 +197,7 @@
attributes_for_samples = {
"sample_template.zpt": {},
- "metal_template.zpt": {"use_macro": "page"},
+ "metal_template.zpt": {},
}
addCompositeForm = PageTemplateFile("addCompositeForm", _www)
@@ -216,7 +215,6 @@
if attrs is None:
raise ValueError("Not allowed: %s" % create_sample)
ob = dispatcher.this()._getOb(ob.getId())
- ob.use_macro = attrs.get("use_macro", "")
f = open(os.path.join(_www, create_sample), "rt")
try:
text = f.read()
=== Products/CompositePage/macro.py 1.4 => 1.5 ===
--- Products/CompositePage/macro.py:1.4 Fri Dec 26 15:43:30 2003
+++ Products/CompositePage/macro.py Wed Dec 31 12:32:14 2003
@@ -25,6 +25,7 @@
from AccessControl.ZopeGuards import guarded_getattr
from TAL.TALInterpreter import TALInterpreter
from Products.PageTemplates.Expressions import getEngine, SecureModuleImporter
+from Products.PageTemplates.PageTemplate import PageTemplate
from interfaces import ISlot
@@ -69,22 +70,18 @@
return getEngine().getContext(c)
-def renderMacro(template, macro_name, composite=None, options=None):
+def renderMacro(template, macro, composite=None, options=None):
"""Renders a page template macro, filling slots.
"""
- 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
program = [
('version', '1.4'),
('mode', 'html'),
- ('setSourceFile', path),
('useMacro', ('macro', expr, slot_generator, [])),
]
@@ -94,6 +91,27 @@
return output.getvalue()
-def getDocumentMacroName(program):
- raise NotImplementedError
+def getRootMacro(template):
+ """If the template defines a macro at the root, returns it.
+ Otherwise returns None.
+ """
+ try:
+ t = isinstance(template, PageTemplate)
+ except TypeError:
+ # Python 2.1 workaround
+ t = 0
+ if not t:
+ return None
+ if not template._v_cooked:
+ template._cook()
+ # Look for a macro at the template's root element.
+ # XXX This would be a lot less hackish with adapters.
+ for (opcode, args) in template._v_program:
+ if opcode in ('version', 'mode', 'setSourceFile', 'setPosition'):
+ continue
+ elif opcode == 'defineMacro':
+ return args[1]
+ else:
+ # Some other opcode.
+ return None
=== Products/CompositePage/render.py 1.3 => 1.4 ===
--- Products/CompositePage/render.py:1.3 Tue Dec 30 15:02:46 2003
+++ Products/CompositePage/render.py Wed Dec 31 12:32:14 2003
@@ -84,7 +84,7 @@
names = self.listAllowableInlineViews()
if names:
name = names[0]
- if name != "call":
+ if name and name != "call":
view = ob.restrictedTraverse(str(name))
return view()
# Special view name "call" means to call the object.
More information about the Zope-CVS
mailing list