[Checkins] SVN: grok/branches/regebro-guido-templates/ Updated after feedback from Brandon.

Lennart Regebro regebro at gmail.com
Tue Nov 6 04:46:55 EST 2007


Log message for revision 81563:
  Updated after feedback from Brandon.
  

Changed:
  U   grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt
  U   grok/branches/regebro-guido-templates/src/grok/components.py
  U   grok/branches/regebro-guido-templates/src/grok/ftests/test_grok_functional.py
  U   grok/branches/regebro-guido-templates/src/grok/tests/adapter/classorinterface.py
  U   grok/branches/regebro-guido-templates/src/grok/tests/template/pluggability.py

-=-
Modified: grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt
===================================================================
--- grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt	2007-11-06 05:51:32 UTC (rev 81562)
+++ grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt	2007-11-06 09:46:54 UTC (rev 81563)
@@ -26,15 +26,12 @@
 View named ``Mammoth`` and next to it instantiate a template named
 ``mammoth``, then Grok will use them together.
 
-To enable such automatic association for a new templating language,
-you need to write a subclass of ``grok.components.GrokTemplate``.
-You will need to override three methods.  The ``fromFile`` and
-``fromString`` methods should each return an instance of the new
-template class.  Your ``render`` method should first call
-``self.getTemplate()`` to retrieve the value you returned from either
-of the other methods, and then run the template on the dictionary of
-values returned by ``self.getNamespace()`` and return the resulting
-string.
+To enable such automatic association for a new templating language, you need
+to write a subclass of ``grok.components.GrokTemplate``. You will need to
+override three methods. The ``setFromFilename`` and ``setFromString`` methods
+should each load the template from disk or a given string, depending on
+method. Your ``render`` method should run the template with the dictionary of
+values returned by ``self.getNamespace()`` and return the resulting string.
 
 Here is an example of a minimal page template integration:
 
@@ -42,33 +39,33 @@
 
 class MyPageTemplate(grok.components.GrokTemplate):
 
-    def fromTemplate(self, template):
-        return MyTemplate(template)
+    def setFromString(self, string):
+        self._template = MyTemplate(string)
 
-    def fromFile(self, filename, _prefix=None):
+    def setFromFilename(self, filename, _prefix=None):
         file = open(os.path.join(_prefix, filename))
-        return MyTemplate(file.read())
+        self._template = MyTemplate(file.read())
 
     def render(self, view):
-        return self.getTemplate().render(**self.getNamespace(view))
+        return self._template.render(**self.getNamespace(view))
 
 With this class finished you can create an inline template, like this:
 
 .. code-block:: python
 
->>> class AView(grok.View):
->>>     pass
->>>     
->>> aview = MyPageTemplate('<html><body>Some text</body></html>')
+    class AView(grok.View):
+        pass
+        
+    aview = MyPageTemplate('<html><body>Some text</body></html>')
 
 And also you can create a filebase template, inline:
 
 .. code-block:: python
 
->>> class AView(grok.View):
->>>     pass
->>>     
->>> aview = MyTemplateFile('lasceuax.html')
+    class AView(grok.View):
+        pass
+        
+    aview = MyTemplateFile('lasceuax.html')
 
 
 Templates in the _templates directory

Modified: grok/branches/regebro-guido-templates/src/grok/components.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/components.py	2007-11-06 05:51:32 UTC (rev 81562)
+++ grok/branches/regebro-guido-templates/src/grok/components.py	2007-11-06 09:46:54 UTC (rev 81563)
@@ -138,7 +138,7 @@
                       "Please use view/@@viewname/macros/macroname\n"
                       "View %r, macro %s" % (self, key),
                       DeprecationWarning)
-        return self.template.getTemplate().macros[key]
+        return self.template._template.macros[key]
     
     def url(self, obj=None, name=None):
         # if the first argument is a string, that's the name. There should
@@ -218,7 +218,7 @@
     This provides most of what a page template needs and is a good base for
     writing your own page template"""
     
-    def __init__(self, template=None, filename=None, _prefix=None):
+    def __init__(self, string=None, filename=None, _prefix=None):
 
         # __grok_module__ is needed to make defined_locally() return True for
         # inline templates
@@ -227,16 +227,16 @@
         # __init__ unless you override all of it.
         self.__grok_module__ = martian.util.caller_module()
         
-        if not (template is None) ^ (filename is None):
+        if not (string is None) ^ (filename is None):
             raise AssertionError("You must pass in template or filename, but not both.")
         
-        if template:
-            self._template = self.fromTemplate(template)
+        if string:
+            self.setFromString(string)
         else:
             if _prefix is None:
                 module = sys.modules[self.__grok_module__]
                 _prefix = os.path.dirname(module.__file__)
-            self._template = self.fromFile(filename, _prefix)
+            self.setFromFilename(filename, _prefix)
 
     def __repr__(self):
         return '<%s template in %s>' % (self.__grok_name__,
@@ -263,9 +263,6 @@
         namespace = self.namespace(view)
         namespace.update(view.namespace())
         return namespace
-    
-    def getTemplate(self):
-        return self._template
 
 class TrustedPageTemplate(TrustedAppPT, pagetemplate.PageTemplate):
     pass
@@ -275,24 +272,23 @@
 
 class PageTemplate(GrokTemplate):
     
-    def fromTemplate(self, template):
+    def setFromString(self, string):
         zpt = TrustedPageTemplate()
-        if martian.util.not_unicode_or_ascii(template):
+        if martian.util.not_unicode_or_ascii(string):
             raise ValueError("Invalid page template. Page templates must be "
                              "unicode or ASCII.")
-        zpt.write(template)
-        return zpt
+        zpt.write(string)
+        self._template = zpt
 
-    def fromFile(self, filename, _prefix=None):
-        #_prefix = PageTemplateFile.get_path_from_prefix(_prefix)
-        return TrustedFilePageTemplate(filename, _prefix)
+    def setFromFilename(self, filename, _prefix=None):
+        self._template = TrustedFilePageTemplate(filename, _prefix)
 
     def _initFactory(self, factory):
-        factory.macros = self.getTemplate().macros
+        factory.macros = self._template.macros
 
     def render(self, view):
         namespace = self.getNamespace(view)
-        template = self.getTemplate()
+        template = self._template
         namespace.update(template.pt_getContext())
         return template.pt_render(namespace)
     
@@ -303,7 +299,7 @@
         if _prefix is None:
             module = sys.modules[self.__grok_module__]
             _prefix = os.path.dirname(module.__file__)
-        self._template = self.fromFile(filename, _prefix)
+        self.setFromFilename(filename, _prefix)
     
 class DirectoryResource(directoryresource.DirectoryResource):
     # We subclass this, because we want to override the default factories for

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-11-06 05:51:32 UTC (rev 81562)
+++ grok/branches/regebro-guido-templates/src/grok/ftests/test_grok_functional.py	2007-11-06 09:46:54 UTC (rev 81563)
@@ -50,7 +50,7 @@
 def test_suite():
     suite = unittest.TestSuite()
     for name in ['view', 'staticdir', 'xmlrpc', 'traversal', 'form', 'url',
-                 'security', 'utility', 'catalog', 'admin', 'template']:
+                 'security', 'utility', 'catalog', 'admin']:
         suite.addTest(suiteFromPackage(name))
     return suite
 

Modified: grok/branches/regebro-guido-templates/src/grok/tests/adapter/classorinterface.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/tests/adapter/classorinterface.py	2007-11-06 05:51:32 UTC (rev 81562)
+++ grok/branches/regebro-guido-templates/src/grok/tests/adapter/classorinterface.py	2007-11-06 09:46:54 UTC (rev 81563)
@@ -7,7 +7,7 @@
     ...
   GrokImportError: You can only pass classes or interfaces to grok.context.
 
-  >>> string_context()
+  >>> stringcontext()
   Traceback (most recent call last):
     ...
   GrokImportError: You can only pass classes or interfaces to grok.context.
@@ -32,7 +32,7 @@
     class FunctionContext(object):
         grok.context(a)
 
-def string_context():
+def stringcontext():
     class StringContext(object):
         grok.context('string')
 

Modified: grok/branches/regebro-guido-templates/src/grok/tests/template/pluggability.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/tests/template/pluggability.py	2007-11-06 05:51:32 UTC (rev 81562)
+++ grok/branches/regebro-guido-templates/src/grok/tests/template/pluggability.py	2007-11-06 09:46:54 UTC (rev 81563)
@@ -43,19 +43,19 @@
 
 class MyPageTemplate(grok.components.GrokTemplate):
 
-    def fromTemplate(self, template):
-        return MyTemplate(template)
+    def setFromString(self, string):
+        self._template = MyTemplate(string)
 
-    def fromFile(self, filename, _prefix=None):
+    def setFromFilename(self, filename, _prefix=None):
         file = open(os.path.join(_prefix, filename))
-        return MyTemplate(file.read())
+        self._template = MyTemplate(file.read())
 
     def namespace(self, view):
         # I'll override the default namespace here for testing:
         return {'middle_text': 'is in'}
 
     def render(self, view):
-        return self.getTemplate().render(**self.getNamespace(view))
+        return self._template.render(**self.getNamespace(view))
 
 class MyPageTemplateFactory(grok.GlobalUtility):
 



More information about the Checkins mailing list