[Checkins] SVN: z3c.pt.compat/trunk/ Added binding methods.

Malthe Borch mborch at gmail.com
Sat Sep 13 15:51:15 EDT 2008


Log message for revision 91126:
  Added binding methods.

Changed:
  U   z3c.pt.compat/trunk/README.txt
  U   z3c.pt.compat/trunk/setup.py
  U   z3c.pt.compat/trunk/src/z3c/pt/compat/__init__.py

-=-
Modified: z3c.pt.compat/trunk/README.txt
===================================================================
--- z3c.pt.compat/trunk/README.txt	2008-09-13 19:30:03 UTC (rev 91125)
+++ z3c.pt.compat/trunk/README.txt	2008-09-13 19:51:15 UTC (rev 91126)
@@ -16,3 +16,22 @@
 
 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:
+
+   >>> from z3c.pt.compat import bind_template
+   >>> from z3c.pt.compat import bind_macro
+
+Both function return render-methods that accept keyword arguments
+which will be passed to the template.
+   
+   >>> render = bind_template(template, view)
+   >>> render = bind_macro(template, view, request, macro)
+
+
+
+   

Modified: z3c.pt.compat/trunk/setup.py
===================================================================
--- z3c.pt.compat/trunk/setup.py	2008-09-13 19:30:03 UTC (rev 91125)
+++ z3c.pt.compat/trunk/setup.py	2008-09-13 19:51:15 UTC (rev 91126)
@@ -28,7 +28,7 @@
           'setuptools',
           ],
       extras_require = dict(
-        zpt = ['zope.app.pagetemplate'],
+        zpt = ['zope.app.pagetemplate', 'zope.tal'],
         z3cpt = ['z3c.pt'],
         )
       )

Modified: z3c.pt.compat/trunk/src/z3c/pt/compat/__init__.py
===================================================================
--- z3c.pt.compat/trunk/src/z3c/pt/compat/__init__.py	2008-09-13 19:30:03 UTC (rev 91125)
+++ z3c.pt.compat/trunk/src/z3c/pt/compat/__init__.py	2008-09-13 19:51:15 UTC (rev 91126)
@@ -1,3 +1,22 @@
+##############################################################################
+#
+# 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: __init__.py 81270 2007-10-31 14:02:39Z jukart $
+"""
+__docformat__ = "reStructuredText"
+
+from StringIO import StringIO
 import config
 
 if config.PREFER_Z3C_PT:
@@ -4,11 +23,35 @@
     from z3c.pt.pagetemplate import ViewPageTemplateFile
 
     def bind_template(pt, view):
-        return pt.bind(view)    
+        return pt.bind(view)
+
+    def bind_macro(template, view, request, macro):
+        return template.bind(
+            view, request=request, macro=macro)
+    
 else:
+    from zope.tal.talinterpreter import TALInterpreter
     from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile    
     from zope.app.pagetemplate.viewpagetemplatefile import BoundPageTemplate as \
          bind_template
+
+    def bind_macro(template, view, request, macro):
+        program = template.macros[macro]
+
+        def render(content_type=None, **kwargs):
+            output = StringIO(u'')
+            namespace = template.pt_getContext(
+                view, request, options=kwargs)
+            context = template.pt_getEngineContext(namespace)
+            TALInterpreter(program, None,
+                           context, output, tal=True, showtal=False,
+                           strictinsert=0, sourceAnnotations=False)()
+            if not request.response.getHeader("Content-Type"):
+                request.response.setHeader(
+                    "Content-Type", content_type)
+            return output.getvalue()
+
+        return render
     
 class ViewPageTemplateFile(ViewPageTemplateFile):
-    pass
+    """View page template file."""



More information about the Checkins mailing list