[Checkins] SVN: grok/branches/regebro-guido-templates/ Refactoring of template langauge support after Martijns input.

Lennart Regebro regebro at gmail.com
Sat Oct 20 11:16:14 EDT 2007


Log message for revision 80946:
  Refactoring of template langauge support after Martijns input.
  

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/interfaces.py
  U   grok/branches/regebro-guido-templates/src/grok/meta.py

-=-
Modified: grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt
===================================================================
--- grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt	2007-10-20 13:43:09 UTC (rev 80945)
+++ grok/branches/regebro-guido-templates/doc/minitutorials/template-languages.txt	2007-10-20 15:16:13 UTC (rev 80946)
@@ -27,40 +27,50 @@
             self._template = MyTemplate(html)
             self.__grok_module__ = martian.util.caller_module()
 
-        def _factory_init(self, factory):
+        def _initFactory(self, factory):
             pass
     
-        def default_namespace(self):
-            return {}
+        def namespace(self, view):
+            namespace = {}
+            namespace['request'] = view.request
+            namespace['view'] = view
+            namespace['context'] = view.context
+            # XXX need to check whether we really want to put None here if missing
+            namespace['static'] = view.static
+            
+            return namespace
     
-        def render_template(self, view):
-            namespace = self.getDefaultVariables()
-            namespace.update(view.getTemplateVariables())
-            return self._template.render(**namespace)
+        def render(self, view):
+            return self._template.render(**self.namespace(view))
 
 In the __init__ method of a typical template you pass in what should be
 rendered. This is usually HTML, but can equally well be XML or text, or
-whatever your template renders. Note the line setting self.__grok_module__.
-This is necessary to support inline templates.
+whatever your template renders. Note the line setting
+self.__grok_module__.This is necessary to support inline templates.
 
-The _factory_init method is a call made when setting up views (when the server
+The _initFactory method is a call made when setting up views (when the server
 starts). Basically, you can here initialize the view in ways needed to your
 template language. For example, Zope Page Templates set the macro attribute on
 the view class, so that you can access ZPT macros with the standard syntax of
 "context/@@viewname/macros/themacro". Most likely your class doesn't need to
 do anything here.
 
-The default_namespace method should return a dictionary with variables that
-should be included in the namespace that is specific for your template
-language. By default 'context', 'view', 'request' and 'static' is made
-available by the view. Your language might want to use some more. 
+The namespace method should return a dictionary with variables that should be
+included in the namespace that is specific for your template language. Common
+variables is 'context', 'view', 'request' and 'static', which is what the
+default is if you don't override this method. Your language might want to use
+more or fewer, depending on how the language works. For example, a template
+language like Kid or Templess that doesn't have object attribute access would
+have no use of any of these variables, and instead you need to in each view
+override the namespace() method to pass in exactly the values needed. If your
+template language has attribute access you probably don't need to override
+this method at all.
 
-Lastly, the render_template is the method normally used to render a template
-attached to a view. Here you do whatever necessary to render your template.
-This is usually to call view.default_namespace() and then update the namespace
-with the result of the view-specific "extra_namespace", to let the view add
-more variables to the namespace, and also override the defaults. The above
-example is a reasonable startingpoint for most cases.
+Lastly, render() is the method normally used to render a template attached to
+a view. Here you do whatever necessary to render your template. This is
+usually to call view.namespace() and pass that into the method that renders
+the template. The above example is for Genshi, and something similar would
+probably be used for your template language too.
 
 With this class finished you can create an inline template, like this:
 
@@ -87,15 +97,13 @@
             self._template = MyTemplate(file.read())
             self.__grok_module__ = martian.util.caller_module()
     
-        def render_template(self, view):
-            namespace = self.getDefaultVariables()
-            namespace.update(view.getTemplateVariables())
-            return self._template.render(**namespace)
+        def render(self, view):
+            return self._template.render(**self.namespace(view))
 
-Here _factory_init and default_namespace is left out, as GrokPaeTemplate
-alredy has them as defaults. The __init__ now takes two parameters, filename
-and _prefix, which is the directory in which the file resides. Although there
-is no requirement that these are the parameters used it is a good idea, since
+Here _initFactory() and namespace() is left out, as GrokPageTemplate alredy
+has them as defaults. The __init__ now takes two parameters, filename and
+_prefix, which is the directory in which the file resides. Although there is
+no requirement that these are the parameters used it is a good idea, since
 that makes the next step easier.
 
 Now you can use this filebase template:
@@ -137,10 +145,8 @@
             self._template = MyTemplate(file.read())
             self.__grok_module__ = martian.util.caller_module()
     
-        def render_template(self, view):
-            namespace = self.getDefaultVariables()
-            namespace.update(view.getTemplateVariables())
-            return self._template.render(**namespace)
+        def render(self, view):
+            return self._template.render(**self.namespace(view))
 
 When your module gets grokked, Grok will now pick up on the MyPageTemplateFile
 class, register it as a global utility for templates with the '.mtl' extension

Modified: grok/branches/regebro-guido-templates/src/grok/components.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/components.py	2007-10-20 13:43:09 UTC (rev 80945)
+++ grok/branches/regebro-guido-templates/src/grok/components.py	2007-10-20 15:16:13 UTC (rev 80946)
@@ -122,23 +122,11 @@
         return mapply(self.render, (), self.request)
 
     def _render_template(self):
-        return self.template.render_template(self)
+        return self.template.render(self)
 
-    def default_namespace(self):
-        namespace = {}
-        namespace['request'] = self.request
-        namespace['view'] = self
-        namespace['context'] = self.context
-        # XXX need to check whether we really want to put None here if missing
-        namespace['static'] = self.static
-        
-        # Get template langage specific defaults:
-        namespace.update(self.template.default_namespace())
-        return namespace
+    def namespace(self):
+        return self.template.namespace()        
 
-    def extra_namespace(self):
-        return {}
-
     def __getitem__(self, key):
         if getattr(self.template, 'macros', None) is None:
             raise AttributeError("View has no item %s" % key)
@@ -215,11 +203,18 @@
         self.__grok_name__ = name
         self.__grok_location__ = location
 
-    def _factory_init(self, factory):
+    def _initFactory(self, factory):
         pass
 
-    def default_namespace(self):
-        return {}
+    def namespace(self, view):
+        namespace = {}
+        namespace['request'] = view.request
+        namespace['view'] = view
+        namespace['context'] = view.context
+        # XXX need to check whether we really want to put None here if missing
+        namespace['static'] = view.static
+        
+        return namespace
 
 
 class PageTemplate(GrokPageTemplate, TrustedAppPT, pagetemplate.PageTemplate):
@@ -238,16 +233,16 @@
         # PageTemplate cannot be subclassed
         self.__grok_module__ = martian.util.caller_module()
 
-    def _factory_init(self, factory):
+    def _initFactory(self, factory):
         factory.macros = self.macros
 
-    def default_namespace(self):
-        return self.pt_getContext()
+    def namespace(self, view):
+        namespace = GrokPageTemplate.namespace(self, view)
+        namespace.update(self.pt_getContext())
+        return namespace
 
-    def render_template(self, view):
-        namespace = view.default_namespace()
-        namespace.update(view.extra_namespace())
-        return self.pt_render(namespace)
+    def render(self, view):
+        return self.pt_render(self.namespace(view))
 
 
 class PageTemplateFile(GrokPageTemplate, TrustedAppPT,
@@ -265,16 +260,16 @@
         # PageTemplateFile cannot be subclassed
         self.__grok_module__ = martian.util.caller_module()
     
-    def _factory_init(self, factory):
+    def _initFactory(self, factory):
         factory.macros = self.macros
 
-    def default_namespace(self):
-        return self.pt_getContext()
+    def namespace(self, view):
+        namespace = GrokPageTemplate.namespace(self, view)
+        namespace.update(self.pt_getContext())
+        return namespace
 
-    def render_template(self, view):
-        namespace = view.default_namespace()
-        namespace.update(view.extra_namespace())
-        return self.pt_render(namespace)
+    def render(self, view):
+        return self.pt_render(self.namespace(view))
 
     
 class DirectoryResource(directoryresource.DirectoryResource):

Modified: grok/branches/regebro-guido-templates/src/grok/interfaces.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/interfaces.py	2007-10-20 13:43:09 UTC (rev 80945)
+++ grok/branches/regebro-guido-templates/src/grok/interfaces.py	2007-10-20 15:16:13 UTC (rev 80946)
@@ -444,12 +444,12 @@
     """Template objects created from files
     """
     
-    def _factory_init(factory):
+    def _initFactory(factory):
         """Template language specific initializations on the view factory."""
     
-    def default_namespace():
+    def namespace():
         """Returns a dictionary of template language specific variables."""
     
-    def render_template(view):
+    def render(view):
         """Renders the template"""
         
\ No newline at end of file

Modified: grok/branches/regebro-guido-templates/src/grok/meta.py
===================================================================
--- grok/branches/regebro-guido-templates/src/grok/meta.py	2007-10-20 13:43:09 UTC (rev 80945)
+++ grok/branches/regebro-guido-templates/src/grok/meta.py	2007-10-20 15:16:13 UTC (rev 80946)
@@ -172,7 +172,7 @@
 
             templates.markAssociated(template_name)
             factory.template = template
-            template._factory_init(factory)
+            template._initFactory(factory)
         else:
             if not getattr(factory, 'render', None):
                 # we do not accept a view without any way to render it



More information about the Checkins mailing list