qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
authorTero Marttila <terom@paivola.fi>
Sun, 14 Sep 2014 13:54:07 +0300
changeset 226 26ec457d0eb2
parent 225 a9bd09144985
child 227 7688463b295d
qmsk_www_pages: pages TemplatePage handling for mako .tmpl pages, with DEBUG-dependent error handling
qmsk_www_pages/pages.py
--- a/qmsk_www_pages/pages.py	Sun Sep 14 13:11:22 2014 +0300
+++ b/qmsk_www_pages/pages.py	Sun Sep 14 13:54:07 2014 +0300
@@ -6,10 +6,14 @@
 import os, os.path
 
 import markdown
+import mako.template
 
 class NotFound (Exception):
     pass
 
+class RenderError (Exception):
+    pass
+
 class Site (object):
     @classmethod
     def lookup (cls):
@@ -342,12 +346,27 @@
             output_format   = self.format,
         )
 
+class TemplatePage (Page):
+    def render_html (self, request):
+        """
+            Raises RenderError if !DEBUG, arbitrary error with stack trace otherwise.
+        """
+
+        try:
+            return mako.template.Template(filename=self.path).render()
+        except Exception as error:
+            if settings.DEBUG:
+                raise
+            else:
+                raise RenderError(error)
+
 SITE = Site.lookup()
 
 TYPES = {
     'html':         HTML_Page,
     'md':           MarkdownPage,
     'markdown':     MarkdownPage,
+    'tmpl':         TemplatePage,
 }
 
 def page (page):