[Checkins] SVN: five.pt/trunk/ Added monkey-patches for module-level view page template instances.

Malthe Borch mborch at gmail.com
Sun Nov 16 17:43:40 EST 2008


Log message for revision 93022:
  Added monkey-patches for module-level view page template instances.

Changed:
  U   five.pt/trunk/CHANGES.txt
  U   five.pt/trunk/src/five/pt/configure.zcml
  A   five.pt/trunk/src/five/pt/patches.py

-=-
Modified: five.pt/trunk/CHANGES.txt
===================================================================
--- five.pt/trunk/CHANGES.txt	2008-11-16 22:21:19 UTC (rev 93021)
+++ five.pt/trunk/CHANGES.txt	2008-11-16 22:43:39 UTC (rev 93022)
@@ -4,6 +4,9 @@
 HEAD
 ----
 
+- Added monkey-patches to replace template engine for module-level
+  view page template instances. [malthe]
+
 - Made `EContext` class more robust. [malthe]
 
 - Register custom file-system page template class for use with CMF

Modified: five.pt/trunk/src/five/pt/configure.zcml
===================================================================
--- five.pt/trunk/src/five/pt/configure.zcml	2008-11-16 22:21:19 UTC (rev 93021)
+++ five.pt/trunk/src/five/pt/configure.zcml	2008-11-16 22:43:39 UTC (rev 93022)
@@ -1,7 +1,9 @@
 <configure xmlns="http://namespaces.zope.org/zope">
 
   <include package="five.pt" file="meta.zcml" />
+
   <include package="five.pt.cmf" />
+  <include package="five.pt.patches" />
   
   <include package="z3c.pt" />
 

Added: five.pt/trunk/src/five/pt/patches.py
===================================================================
--- five.pt/trunk/src/five/pt/patches.py	                        (rev 0)
+++ five.pt/trunk/src/five/pt/patches.py	2008-11-16 22:43:39 UTC (rev 93022)
@@ -0,0 +1,29 @@
+"""Monkey-patching page template classes.
+
+Since many templates are instantiated at module-import, we patch using
+a duck-typing strategy.
+
+We replace the ``__get__``-method of the ViewPageTemplateFile class
+(both the Five variant and the base class). This allows us to return a
+Chameleon template instance, transparent to the calling class.
+"""
+
+from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile as \
+     ZopeViewPageTemplateFile
+
+from Products.Five.browser.pagetemplatefile import BoundPageTemplate
+from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile as \
+     FiveViewPageTemplateFile
+     
+from five.pt.pagetemplate import ViewPageTemplateFile
+
+_marker = object()
+
+def get_bound_template(self, instance, type):
+    template = getattr(self, '_template', _marker)
+    if template is _marker:
+        self._template = template = ViewPageTemplateFile(self.filename)
+    return BoundPageTemplate(template, instance)
+
+FiveViewPageTemplateFile.__get__ = get_bound_template
+ZopeViewPageTemplateFile.__get__ = get_bound_template



More information about the Checkins mailing list