lib/template.py
branchsites
changeset 32 be954df4f0e8
parent 26 9d3beac1b196
child 40 71ab68f31a1c
--- a/lib/template.py	Sat Feb 07 16:33:27 2009 +0200
+++ b/lib/template.py	Sat Feb 07 16:55:23 2009 +0200
@@ -5,7 +5,7 @@
 # use Mako
 from mako import exceptions
 from mako.template import Template
-from mako.lookup import TemplateLookup
+import mako.lookup
 
 # for http.ResponseError
 import http
@@ -21,11 +21,6 @@
 # template file extension
 TEMPLATE_EXT = "tmpl"
 
-
-# our Mako template lookup handler
-_lookup = TemplateLookup(directories=[TEMPLATE_DIR], module_directory=CACHE_DIR)
-
-
 class TemplateError (http.ResponseError) :
     """
         Raised by the template module functions
@@ -33,29 +28,7 @@
 
     pass
 
-def lookup (name) :
-    """
-        Looks up a template based on the bare "name", which does not include the path or file extension
-    """
-    
-    try :
-        return _lookup.get_template("%s.%s" % (name, TEMPLATE_EXT))
-
-    except :
-        raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
-
-def load (path) :
-    """
-        Loads a template from a specific file
-    """
-
-    try :
-        return Template(filename=path, module_directory=CACHE_DIR)
-
-    except :
-        raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
-
-def render_template (tpl, **params) :
+def render (tpl, **params) :
     """
         Render the given template, returning the output as a unicode string, or raising a TemplateError
     """
@@ -78,17 +51,59 @@
 
         raise TemplateError("Template render failed", status='500 Internal Server Error', details=details)
 
-def render (name, **params) :
+class TemplateLoader (mako.lookup.TemplateLookup) :
     """
-        Render a template, using lookup() on the given name
+        Our own specialization of mako's TemplateLookup
     """
 
-    return render_template(lookup(name), **params)
+    def __init__ (self, path, fileext=TEMPLATE_EXT) :
+        """
+            Initialize to load templates located at path, with the given file extension
+        """
 
-def render_file (path, **params) :
-    """
-        Render a template, using load() on the given path
-    """
+        # store
+        self.path = path
+        self.fileext = fileext
+        
+        # XXX: separate cache?
+        super(TemplateLoader, self).__init__(directories=[path], module_directory=CACHE_DIR)
 
-    return render_template(load(path), **params)
+    def lookup (self, name) :
+        """
+            Looks up a template based on the bare "name", which does not include the path or file extension
+        """
+        
+        try :
+            return self.get_template("%s.%s" % (name, self.fileext))
 
+        except :
+            raise TemplateError("Template broken: %r" % (name, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
+    
+    def render (name, **params) :
+        """
+            Render a template, using lookup() on the given name
+        """
+
+        return render(self.lookup(name), **params)
+
+    @classmethod
+    def load (cls, path) :
+        """
+            Loads a template from a specific file
+        """
+
+        try :
+            return Template(filename=path, module_directory=CACHE_DIR)
+
+        except :
+            raise TemplateError("Template broken: %r" % (path, ), status='500 Internal Server Error', details=exceptions.text_error_template().render())
+    
+    @classmethod
+    def render_file (cls, path, **params) :
+        """
+            Render a template, using load() on the given path
+        """
+
+        return render(cls.load(path), **params)
+
+