[Checkins] SVN: z3c.ptcompat/trunk/ Added module which patches 'zope.app.pagetemplate' such that template classes depend on 'z3c.pt' for rendering (import optional).

Malthe Borch mborch at gmail.com
Mon Feb 16 06:57:36 EST 2009


Log message for revision 96587:
  Added module which patches 'zope.app.pagetemplate' such that template classes depend on 'z3c.pt' for rendering (import optional).

Changed:
  U   z3c.ptcompat/trunk/CHANGES.txt
  U   z3c.ptcompat/trunk/README.txt
  A   z3c.ptcompat/trunk/src/z3c/ptcompat/patches/
  A   z3c.ptcompat/trunk/src/z3c/ptcompat/patches/__init__.py
  A   z3c.ptcompat/trunk/src/z3c/ptcompat/patches/configure.zcml

-=-
Modified: z3c.ptcompat/trunk/CHANGES.txt
===================================================================
--- z3c.ptcompat/trunk/CHANGES.txt	2009-02-16 11:10:29 UTC (rev 96586)
+++ z3c.ptcompat/trunk/CHANGES.txt	2009-02-16 11:57:36 UTC (rev 96587)
@@ -1,6 +1,10 @@
 Changelog
 ---------
 
+- Added module which patches ``zope.app.pagetemplate`` such that
+  template classes depend on ``z3c.pt`` for rendering (import
+  optional). [malthe]
+
 0.4 (released 2/10/2009)
 ~~~~~~~~~~~~~~~~~~~~~~~~
 

Modified: z3c.ptcompat/trunk/README.txt
===================================================================
--- z3c.ptcompat/trunk/README.txt	2009-02-16 11:10:29 UTC (rev 96586)
+++ z3c.ptcompat/trunk/README.txt	2009-02-16 11:57:36 UTC (rev 96587)
@@ -7,23 +7,21 @@
  * z3c.pt
  * zope.pagetemplate
 
-This package superseeds and replaces the old z3c.pt.compat, please use
-z3c.ptcompat instead.
+If the environment-variable ``PREFER_Z3C_PT`` is set to a true value,
+the ``z3c.pt`` engine will be used instead of ``zope.pagetemplate``.
 
+Note: This package superseeds and replaces the old z3c.pt.compat,
+please use z3c.ptcompat instead.
+
 Usage
 -----
 
-Import page template classes directly from this package:
+When writing new code or modifying existing code, import page template
+classes from the ``z3c.ptcompat`` package:
 
   >>> from z3c.ptcompat import PageTemplateFile
   >>> from z3c.ptcompat import ViewPageTemplateFile
 
-If the environment-variable ``PREFER_Z3C_PT`` is set to a true value,
-the ``z3c.pt`` engine will be used instead of ``zope.pagetemplate``.
-
-Binding methods
----------------
-
 Two methods are available to bind templates and template macros to a
 view:
 
@@ -36,6 +34,12 @@
    >>> render = bind_template(template, view)
    >>> render = bind_macro(template, view, request, macro)
 
+Patches
+-------
 
+By loading the ``patches`` module, Zope view page template classes
+will be monkey-patched to use the ``z3c.pt`` rendering facilities:
 
-   
+  <include package="z3c.ptcompat.patches" />
+
+This is an alternative to changing module import locations.

Added: z3c.ptcompat/trunk/src/z3c/ptcompat/patches/__init__.py
===================================================================
--- z3c.ptcompat/trunk/src/z3c/ptcompat/patches/__init__.py	                        (rev 0)
+++ z3c.ptcompat/trunk/src/z3c/ptcompat/patches/__init__.py	2009-02-16 11:57:36 UTC (rev 96587)
@@ -0,0 +1,30 @@
+"""Loading this module will monkey-patch ZPT template classes such
+that they render using Chameleon.
+
+Since many templates are instantiated at module-import, we patch using
+a duck-typing strategy.
+
+We replace the ``__get__``-method of the ViewPageTemplateFile
+class. This allows us to return a Chameleon template instance,
+transparent to the calling class.
+"""
+
+from zope.app.pagetemplate.viewpagetemplatefile import BoundPageTemplate
+from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile as \
+     ZopeViewPageTemplateFile
+
+from z3c.pt.pagetemplate import ViewPageTemplateFile
+
+_marker = object()
+
+def get_bound_template(self, instance, type):
+    if instance is None:
+        return self
+        
+    template = getattr(self, '_template', _marker)
+    if template is _marker:
+        self._template = template = ViewPageTemplateFile(self.filename)
+
+    return template.bind(instance)
+
+ZopeViewPageTemplateFile.__get__ = get_bound_template

Added: z3c.ptcompat/trunk/src/z3c/ptcompat/patches/configure.zcml
===================================================================
--- z3c.ptcompat/trunk/src/z3c/ptcompat/patches/configure.zcml	                        (rev 0)
+++ z3c.ptcompat/trunk/src/z3c/ptcompat/patches/configure.zcml	2009-02-16 11:57:36 UTC (rev 96587)
@@ -0,0 +1 @@
+<configure xmlns="http://namespaces.zope.org/zope" />



More information about the Checkins mailing list