# HG changeset patch # User Tero Marttila # Date 1234018523 -7200 # Node ID be954df4f0e837db5730478964ad3462bebce9fa # Parent 107062ebb6f932ec9b67965dc91683c111316a48 move layout.tmpl to sites/www.qmsk.net diff -r 107062ebb6f9 -r be954df4f0e8 lib/filesystem/map.py --- a/lib/filesystem/map.py Sat Feb 07 16:33:27 2009 +0200 +++ b/lib/filesystem/map.py Sat Feb 07 16:55:23 2009 +0200 @@ -16,13 +16,14 @@ (template.TEMPLATE_EXT, page.TemplatePage ), ] - def __init__ (self, path) : + def __init__ (self, path, template) : """ Create, path is where the pages are stored. The list of pages is loaded from $path/list """ # store - self.path = path + self.path = path + self.template = template # load the page tree self.tree = page_tree.PageTree(path + '/list') @@ -118,7 +119,7 @@ p.bind_request(request) # render the template - response_data = template.render("layout", + response_data = template.render(self.template, site_root_url = request.get_script_dir(), site_page_url = request.get_page_prefix(), page = p, diff -r 107062ebb6f9 -r be954df4f0e8 lib/filesystem/page.py --- a/lib/filesystem/page.py Sat Feb 07 16:33:27 2009 +0200 +++ b/lib/filesystem/page.py Sat Feb 07 16:55:23 2009 +0200 @@ -125,7 +125,7 @@ Loads the .tmpl file, and renders it """ - return template.render_file(self.path, + return template.TemplateLoader.render_file(self.path, request = self.request, page_tree = self.fs.tree ) diff -r 107062ebb6f9 -r be954df4f0e8 lib/map.py --- a/lib/map.py Sat Feb 07 16:33:27 2009 +0200 +++ b/lib/map.py Sat Feb 07 16:55:23 2009 +0200 @@ -68,7 +68,7 @@ # store self.mappings = mappings - def map_request (self, request) : + def handle_request (self, request) : """ Returns the appropriate handler """ diff -r 107062ebb6f9 -r be954df4f0e8 lib/template.py --- 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) + + diff -r 107062ebb6f9 -r be954df4f0e8 sites/irclogs.qmsk.net/__init__.py --- a/sites/irclogs.qmsk.net/__init__.py Sat Feb 07 16:33:27 2009 +0200 +++ b/sites/irclogs.qmsk.net/__init__.py Sat Feb 07 16:55:23 2009 +0200 @@ -3,5 +3,8 @@ """ # the URL mapper -from urls import build_mapper +import urls +# our RequestHandler +handler = urls.build_mapper() + diff -r 107062ebb6f9 -r be954df4f0e8 sites/www.qmsk.net/__init__.py --- a/sites/www.qmsk.net/__init__.py Sat Feb 07 16:33:27 2009 +0200 +++ b/sites/www.qmsk.net/__init__.py Sat Feb 07 16:55:23 2009 +0200 @@ -2,8 +2,9 @@ The www.qmsk.net site is just a simple site with a filesystem-based URL mapping """ +from lib import template from lib.filesystem.map import FilesystemMapper as _fstree # global mapper attribute -handler = _fstree("pages") +handler = _fstree("pages", template=template.TemplateLoader.load("sites/www.qmsk.net/templates/layout.tmpl")) diff -r 107062ebb6f9 -r be954df4f0e8 sites/www.qmsk.net/templates/layout.tmpl --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sites/www.qmsk.net/templates/layout.tmpl Sat Feb 07 16:55:23 2009 +0200 @@ -0,0 +1,56 @@ + + +<%def name="render_menu(open_page, page, items, ancestry)"> + + + + + + qmsk.net ${' :: ' + h.breadcrumb(menu.ancestry, links=False) if menu.ancestry else ''} + + + + + +
+ + +
+ + ${page.content} +
+ +
+ + + + + diff -r 107062ebb6f9 -r be954df4f0e8 templates/layout.tmpl --- a/templates/layout.tmpl Sat Feb 07 16:33:27 2009 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,56 +0,0 @@ - - -<%def name="render_menu(open_page, page, items, ancestry)"> - - - - - - qmsk.net ${' :: ' + h.breadcrumb(menu.ancestry, links=False) if menu.ancestry else ''} - - - - - -
- - -
- - ${page.content} -
- -
- - - - -