[Checkins] SVN: grok/branches/regebro-guido-templates/ Corrected one or two typos in the "template-languages" minitutorial,

Brandon Rhodes brandon at rhodesmill.org
Sat Oct 27 21:20:09 EDT 2007


Log message for revision 81152:
  Corrected one or two typos in the "template-languages" minitutorial,
  and added two basic functional tests that verify that the approach of
  the minitutorial works.
  

Changed:
  U   grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt
  A   grok/branches/regebro-guido-templates/src/grok/ftests/template/
  A   grok/branches/regebro-guido-templates/src/grok/ftests/template/__init__.py
  A   grok/branches/regebro-guido-templates/src/grok/ftests/template/language_file.py
  A   grok/branches/regebro-guido-templates/src/grok/ftests/template/language_file.txt
  A   grok/branches/regebro-guido-templates/src/grok/ftests/template/language_inline.py
  U   grok/branches/regebro-guido-templates/src/grok/ftests/test_grok_functional.py

-=-
Modified: grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt
===================================================================
--- grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt	2007-10-28 01:10:00 UTC (rev 81151)
+++ grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt	2007-10-28 01:20:08 UTC (rev 81152)
@@ -95,7 +95,7 @@
     class MyPageTemplateFile(grok.components.GrokPageTemplate):
 
         def __init__(self, filename, _prefix=None):
-            file = open(os.path.join(_prefix, filename)
+            file = open(os.path.join(_prefix, filename))
             self._template = MyTemplate(file.read())
             self.__grok_module__ = martian.util.caller_module()
     
@@ -115,7 +115,7 @@
     class AView(grok.View):
         pass
         
-    aview = MyTemplateFile('lasceuax.html', '.')
+    aview = MyPageTemplateFile('lasceuax.html', '.')
 
 
 Templates in the _templates directory

Added: grok/branches/regebro-guido-templates/src/grok/ftests/template/__init__.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/ftests/template/__init__.py	                        (rev 0)
+++ grok/branches/regebro-guido-templates/src/grok/ftests/template/__init__.py	2007-10-28 01:20:08 UTC (rev 81152)
@@ -0,0 +1 @@
+# this is a package

Added: grok/branches/regebro-guido-templates/src/grok/ftests/template/language_file.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/ftests/template/language_file.py	                        (rev 0)
+++ grok/branches/regebro-guido-templates/src/grok/ftests/template/language_file.py	2007-10-28 01:20:08 UTC (rev 81152)
@@ -0,0 +1,71 @@
+"""
+Containers can determine how they want to be traversed by
+implementing a 'traverse' method, but the behavior falls back to
+basic container traversal if the 'traverse' method returns None:
+
+  >>> getRootFolder()["bear"] = Bear('Bjorn')
+
+Let's first try to look up the special traversed item:
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+  >>> browser.open("http://localhost/bear")
+  >>> print browser.contents
+  ME GROK STILL SEE BEAR NAMED Bjorn!
+
+"""
+import grok
+import martian
+import os.path
+
+class PercentTemplate(object):
+    """A simple template class that does Python %-substitution."""
+    def __init__(self, text):
+        self.text = text
+
+    def render(self, **namespace):
+        return self.text % namespace
+
+class PercentPageTemplate(grok.components.GrokPageTemplate):
+    """Glue class suggested by doc/minitutorials/template-languages.txt."""
+    def __init__(self, html):
+        self._template = PercentTemplate(html)
+        self.__grok_module__ = martian.util.caller_module()
+
+    def _initFactory(self, factory):
+        pass
+    
+    def namespace(self, view):
+        namespace = {}
+        namespace['request'] = view.request
+        namespace['view'] = view
+        namespace['context'] = view.context
+        namespace['static'] = view.static
+        return namespace
+    
+    def render(self, view):
+        namespace = self.namespace(view)
+        namespace.update(view.namespace())        
+        return self._template.render(**namespace)
+
+class PercentPageTemplateFile(grok.components.GrokPageTemplate):
+    """Glue class suggested by doc/minitutorials/template-languages.txt."""
+    def __init__(self, filename, _prefix=None):
+        file = open(os.path.join(_prefix, filename))
+        self._template = PercentPageTemplate(file.read())
+        self.__grok_module__ = martian.util.caller_module()
+
+    def render(self, view):
+        return self._template.render(view)
+
+class Bear(grok.Model):
+    def __init__(self, name):
+        self.name = name
+
+class Index(grok.View):
+    def namespace(self):
+        return { 'bear_name': self.context.name }
+
+index = PercentPageTemplateFile('language_file.txt',
+                                os.path.dirname(__file__))

Added: grok/branches/regebro-guido-templates/src/grok/ftests/template/language_file.txt
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/ftests/template/language_file.txt	                        (rev 0)
+++ grok/branches/regebro-guido-templates/src/grok/ftests/template/language_file.txt	2007-10-28 01:20:08 UTC (rev 81152)
@@ -0,0 +1 @@
+ME GROK STILL SEE BEAR NAMED %(bear_name)s!

Added: grok/branches/regebro-guido-templates/src/grok/ftests/template/language_inline.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/ftests/template/language_inline.py	                        (rev 0)
+++ grok/branches/regebro-guido-templates/src/grok/ftests/template/language_inline.py	2007-10-28 01:20:08 UTC (rev 81152)
@@ -0,0 +1,61 @@
+"""
+Containers can determine how they want to be traversed by
+implementing a 'traverse' method, but the behavior falls back to
+basic container traversal if the 'traverse' method returns None:
+
+  >>> getRootFolder()["bear"] = Bear('Bjorn')
+
+Let's first try to look up the special traversed item:
+
+  >>> from zope.testbrowser.testing import Browser
+  >>> browser = Browser()
+  >>> browser.handleErrors = False
+  >>> browser.open("http://localhost/bear")
+  >>> print browser.contents
+  ME GROK SEE BEAR NAMED Bjorn!
+
+"""
+import grok
+import martian
+
+class PercentTemplate(object):
+    """A simple template class that does Python %-substitution."""
+    def __init__(self, text):
+        self.text = text
+
+    def render(self, **namespace):
+        return self.text % namespace
+
+class PercentPageTemplate(grok.components.GrokPageTemplate):
+    """Glue class suggested by doc/minitutorials/template-languages.txt."""
+    def __init__(self, html):
+        self._template = PercentTemplate(html)
+        self.__grok_module__ = martian.util.caller_module()
+
+    def _initFactory(self, factory):
+        pass
+    
+    def namespace(self, view):
+        namespace = {}
+        namespace['request'] = view.request
+        namespace['view'] = view
+        namespace['context'] = view.context
+        namespace['static'] = view.static
+        return namespace
+    
+    def render(self, view):
+        namespace = self.namespace(view)
+        namespace.update(view.namespace())        
+        return self._template.render(**namespace)
+
+class Bear(grok.Model):
+    def __init__(self, name):
+        self.name = name
+
+class Index(grok.View):
+    def namespace(self):
+        return { 'bear_name': self.context.name }
+
+index = PercentPageTemplate(
+    "ME GROK SEE BEAR NAMED %(bear_name)s!"
+    )

Modified: grok/branches/regebro-guido-templates/src/grok/ftests/test_grok_functional.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/ftests/test_grok_functional.py	2007-10-28 01:10:00 UTC (rev 81151)
+++ grok/branches/regebro-guido-templates/src/grok/ftests/test_grok_functional.py	2007-10-28 01:20:08 UTC (rev 81152)
@@ -50,7 +50,7 @@
 def test_suite():
     suite = unittest.TestSuite()
     for name in ['view', 'staticdir', 'xmlrpc', 'traversal', 'form', 'url',
-                 'security', 'utility', 'catalog', 'admin']:
+                 'security', 'utility', 'catalog', 'admin', 'template']:
         suite.addTest(suiteFromPackage(name))
     return suite
 



More information about the Checkins mailing list