[Checkins] SVN: five.grok/trunk/src/five/grok/ Support inline templates, Zope 2 style

Martin Aspeli optilude at gmx.net
Sun Aug 3 20:05:31 EDT 2008


Log message for revision 89327:
  Support inline templates, Zope 2 style
  

Changed:
  U   five.grok/trunk/src/five/grok/README.txt
  U   five.grok/trunk/src/five/grok/__init__.py
  U   five.grok/trunk/src/five/grok/components.py

-=-
Modified: five.grok/trunk/src/five/grok/README.txt
===================================================================
--- five.grok/trunk/src/five/grok/README.txt	2008-08-03 23:55:31 UTC (rev 89326)
+++ five.grok/trunk/src/five/grok/README.txt	2008-08-04 00:05:31 UTC (rev 89327)
@@ -64,6 +64,17 @@
     ...             cavesInfos.append(caveInfo)
     ...         return cavesInfos
 
+The example above uses a filesystem template. We can also use inline
+templates, like this:
+
+    <<< class InlineGrokVillage(grok.View):
+    ...     grok.context(GrokVillage)
+    ...     grok.name('inline')
+
+    <<< inlinegrokvillage = grok.PageTemplate(u'Village: <b tal:content="context/id"></b>')
+
+Or, we could specify the render() method explicitly:
+
     <<< class Cave(SimpleFolder):
     ...
     ...     def numberOfCaveWomen(self):
@@ -228,3 +239,6 @@
     </body>
     </html>
     <BLANKLINE>
+    
+    >>> print queryMultiAdapter((village, request), name='inline')()
+    Village: <b>amsterdam</b>
\ No newline at end of file

Modified: five.grok/trunk/src/five/grok/__init__.py
===================================================================
--- five.grok/trunk/src/five/grok/__init__.py	2008-08-03 23:55:31 UTC (rev 89326)
+++ five.grok/trunk/src/five/grok/__init__.py	2008-08-04 00:05:31 UTC (rev 89327)
@@ -3,4 +3,4 @@
 from grokcore.view import *
 
 from five.grok.components import View, Model
-from five.grok.components import ZopeTwoPageTemplate
\ No newline at end of file
+from five.grok.components import ZopeTwoPageTemplate as PageTemplate
\ No newline at end of file

Modified: five.grok/trunk/src/five/grok/components.py
===================================================================
--- five.grok/trunk/src/five/grok/components.py	2008-08-03 23:55:31 UTC (rev 89326)
+++ five.grok/trunk/src/five/grok/components.py	2008-08-04 00:05:31 UTC (rev 89327)
@@ -20,10 +20,53 @@
 class View(grokcore.view.View, Acquisition.Explicit):
     pass
 
+# TODO: This should probably move to Products.Five.browser
+
+from Acquisition import aq_inner
+from Products.PageTemplates.ZopePageTemplate import ZopePageTemplate
+from Products.PageTemplates.Expressions import SecureModuleImporter
+from Products.Five.browser.pagetemplatefile import getEngine
+from zope.app.pagetemplate.viewpagetemplatefile import ViewMapper
+
+class ViewAwareZopePageTemplate(ZopePageTemplate):
+    
+    def pt_getEngine(self):
+        return getEngine()
+    
+    def pt_getContext(self):
+        try:
+            root = self.getPhysicalRoot()
+        except AttributeError:
+            try:
+                root = self.context.getPhysicalRoot()
+            except AttributeError:
+                root = None
+
+        view = self._getContext()
+        here = aq_inner(self.context)
+
+        request = getattr(root, 'REQUEST', None)
+        c = {'template': self,
+             'here': here,
+             'context': here,
+             'container': here,
+             'nothing': None,
+             'options': {},
+             'root': root,
+             'request': request,
+             'modules': SecureModuleImporter,
+             }
+        if view is not None:
+            c['view'] = view
+            c['views'] = ViewMapper(here, request)
+
+        return c
+
+
 class ZopeTwoPageTemplate(PageTemplate):
 
     def setFromString(self, string):
-        raise NotImplemented
+        self._template = ViewAwareZopePageTemplate(id=None, text=string)
 
     def setFromFilename(self, filename, _prefix=None):
         self._template = ViewPageTemplateFile(filename, _prefix)



More information about the Checkins mailing list