changeset 695:ed5044d318ed trunk

The `TemplateLoader` class now provides an `instantiate()` method that can be overridden by subclasses to implement advanced template instantiation logic. Fixes #204.
author cmlenz
date Wed, 26 Mar 2008 22:22:47 +0000
parents 07e3f6f0ef57
children 66eead58c120
files ChangeLog genshi/template/loader.py
diffstat 2 files changed, 33 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog
+++ b/ChangeLog
@@ -56,6 +56,9 @@
    including) the matching template itself are applied to the matched content,
    whereas the match templates declared after the matching template are only
    applied to the generated content (ticket #186).
+ * The `TemplateLoader` class now provides an `instantiate()` method that can
+   be overridden by subclasses to implement advanced template instantiation
+   logic (ticket #204).
 
 
 Version 0.4.4
--- a/genshi/template/loader.py
+++ b/genshi/template/loader.py
@@ -155,13 +155,11 @@
         :param encoding: the encoding of the template to load; defaults to the
                          ``default_encoding`` of the loader instance
         :return: the loaded `Template` instance
-        :raises TemplateNotFound: if a template with the given name could not be
-                                  found
+        :raises TemplateNotFound: if a template with the given name could not
+                                  be found
         """
         if cls is None:
             cls = self.default_class
-        if encoding is None:
-            encoding = self.default_encoding
         if relative_to and not os.path.isabs(relative_to):
             filename = os.path.join(os.path.dirname(relative_to), filename)
         filename = os.path.normpath(filename)
@@ -210,10 +208,8 @@
                             # search path
                             filename = os.path.join(dirname, filename)
                             dirname = ''
-                        tmpl = cls(fileobj, basedir=dirname, filename=filename,
-                                   loader=self, encoding=encoding,
-                                   lookup=self.variable_lookup,
-                                   allow_exec=self.allow_exec)
+                        tmpl = self.instantiate(cls, fileobj, dirname,
+                                                filename, encoding=encoding)
                         if self.callback:
                             self.callback(tmpl)
                         self._cache[filename] = tmpl
@@ -228,3 +224,29 @@
 
         finally:
             self._lock.release()
+
+    def instantiate(self, cls, fileobj, dirname, filename, encoding=None):
+        """Instantiate and return the `Template` object based on the given
+        class and parameters.
+        
+        This function is intended for subclasses to override if they need to
+        implement special template instantiation logic. Code that just uses
+        the `TemplateLoader` should use the `load` method instead.
+        
+        :param cls: the class of the template object to instantiate
+        :param fileobj: a readable file-like object containing the template
+                        source
+        :param dirname: the name of the base directory containing the template
+                        file
+        :param filename: the name of the template file, relative to the given
+                         base directory
+        :param encoding: the encoding of the template to load; defaults to the
+                         ``default_encoding`` of the loader instance
+        :return: the loaded `Template` instance
+        :rtype: `Template`
+        """
+        if encoding is None:
+            encoding = self.default_encoding
+        return cls(fileobj, basedir=dirname, filename=filename, loader=self,
+                   encoding=encoding, lookup=self.variable_lookup,
+                   allow_exec=self.allow_exec)
Copyright (C) 2012-2017 Edgewall Software