[Checkins] SVN: five.customerize/trunk/src/five/customerize/ Add
initial classes for TTWTemplate and renderer
Alec Mitchell
apm13 at columbia.edu
Sat Oct 28 19:13:51 EDT 2006
Log message for revision 70955:
Add initial classes for TTWTemplate and renderer
Changed:
A five.customerize/trunk/src/five/customerize/configure.zcml
A five.customerize/trunk/src/five/customerize/tests.py
A five.customerize/trunk/src/five/customerize/zpt.py
A five.customerize/trunk/src/five/customerize/zpt.txt
-=-
Added: five.customerize/trunk/src/five/customerize/configure.zcml
===================================================================
--- five.customerize/trunk/src/five/customerize/configure.zcml 2006-10-28 22:57:47 UTC (rev 70954)
+++ five.customerize/trunk/src/five/customerize/configure.zcml 2006-10-28 23:13:50 UTC (rev 70955)
@@ -0,0 +1,4 @@
+<configure xmlns="http://namespaces.zope.org/zope"
+ xmlns:five="http://namespaces.zope.org/five">
+
+</configure>
\ No newline at end of file
Added: five.customerize/trunk/src/five/customerize/tests.py
===================================================================
--- five.customerize/trunk/src/five/customerize/tests.py 2006-10-28 22:57:47 UTC (rev 70954)
+++ five.customerize/trunk/src/five/customerize/tests.py 2006-10-28 23:13:50 UTC (rev 70955)
@@ -0,0 +1,19 @@
+import unittest
+from Testing.ZopeTestCase import ZopeDocFileSuite
+from zope.traversing.adapters import DefaultTraversable
+import zope.component.testing
+
+__docformat__ = "reStructuredText"
+
+def setUp(test):
+ zope.component.testing.setUp(test)
+ zope.component.provideAdapter(DefaultTraversable, (None,))
+
+def test_suite():
+ return unittest.TestSuite([
+ ZopeDocFileSuite('zpt.txt', package="five.customerize",
+ setUp=setUp, tearDown=zope.component.testing.tearDown),
+ ])
+
+if __name__ == '__main__':
+ unittest.main(defaultTest='test_suite')
Added: five.customerize/trunk/src/five/customerize/zpt.py
===================================================================
--- five.customerize/trunk/src/five/customerize/zpt.py 2006-10-28 22:57:47 UTC (rev 70954)
+++ five.customerize/trunk/src/five/customerize/zpt.py 2006-10-28 23:13:50 UTC (rev 70955)
@@ -0,0 +1,37 @@
+from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
+
+class TTWTemplate(ZopePageTemplate):
+ """A template class used to generate Zope 3 views TTW"""
+
+ def __init__(self, id, text=None, content_type=None, encoding='utf-8',
+ strict=False, view=None):
+ self.view = view
+ super(TTWTemplate, self).__init__(id, text, content_type, encoding,
+ strict)
+
+ def __call__(self, context, request):
+ return TTWTemplateRenderer(context, request, self, self.view)
+
+
+class TTWTemplateRenderer(object):
+ def __init__(self, context, request, template, view):
+ self.context = context
+ self.request = request
+ self.template = template
+ self.view = view
+
+ def __call__(self, *args, **kwargs):
+ """Add the zope user to the security context, as done in
+ PageTemplateFile"""
+ view = self.view
+ if view is not None:
+ class TTWView(view):
+ __allow_access_to_unprotected_subobjects__ = 1
+ view = TTWView(self.context, self.request)
+ bound_names = {'view': view,
+ 'request': self.request,
+ 'context': self.context}
+ return self.template._exec(bound_names, args, kwargs)
+
+ def __of__(self, obj):
+ return self
Added: five.customerize/trunk/src/five/customerize/zpt.txt
===================================================================
--- five.customerize/trunk/src/five/customerize/zpt.txt 2006-10-28 22:57:47 UTC (rev 70954)
+++ five.customerize/trunk/src/five/customerize/zpt.txt 2006-10-28 23:13:50 UTC (rev 70955)
@@ -0,0 +1,26 @@
+TTW Template Tests
+----------------
+
+First we create a simple TTWTemplate object and then we obtain a
+renderer by calling as if it were a view factory:
+
+ >>> from five.customerize.zpt import TTWTemplate
+ >>> template = TTWTemplate('test_template', '<html></html>')
+ >>> template = template.__of__(app)
+ >>> renderer = template(None, None)
+ >>> print renderer()
+ <html></html>
+
+We now add some more complex tal expressions to our template, and
+ensure that it obtains the passed in request and context for rendering:
+
+ >>> template.pt_edit('''\
+ ... <span tal:replace="context/getId"/>
+ ... <span tal:replace="request/foo"/>
+ ... <span tal:replace="python:repr(view)"/>''', 'text/html')
+ >>> request = {'foo': 'bar'}
+ >>> renderer = template(self.folder, request)
+ >>> print renderer()
+ test_folder_1_
+ bar
+ None
More information about the Checkins
mailing list