# # Requires mako # # __all__ = [ "tpl", "Controller", "modules_dir", "templates", "images_url" ] modules_dir = "zcache" templates = ["templates"] images_url = "/static/images/" from copy import copy try: from mako.template import Template from mako.lookup import TemplateLookup from mako import exceptions except: pass try: import web except: pass try: from configuration_base import Config except: class Config: pass def tpl( tpl_name, **kwargs ): ''' Simple handle to parse template ; all other parameters would be transferred into template. Also template monster can find some default parameters like: ${i} = /static/images/ ${Config} = configuration parameters from "configuration_base.Config" (if given) Example: >>> tpl( "test.html", something=value ) ''' C = Controller() return C.tpl( tpl_name, **kwargs ) class Controller: ''' Default controller -- subject to build your own controllers upon. Suggested scheme with GET/_GET division provided: use it to section your "default" controller code from specific routines. NB: Use self.tpl(tpl_name, **params) to output templates. ''' id = 'none' def __init__(self): self.lookup = TemplateLookup(directories=templates, module_directory=modules_dir, output_encoding='utf-8', encoding_errors='replace') self.config = Config() def _GET( self, *args ): web.output("GET") def _POST( self, *args ): web.output("POST") def GET( self, *args ): return self._GET(*args) def POST( self, *args ): return self._POST(*args) def tpl( self, tpl_name, **kwargs): try: kwargs[ "i" ] = images_url kwargs[ "Config" ] = self.config template = self.lookup.get_template(tpl_name) return template.render(**kwargs) except: return exceptions.html_error_template().render() class Static(Controller): ''' Simple static controller, use it with "/(.*)" rule. If renders template with same name as given in URL. Slashes (/) are converted to underscores (_). ''' def _POST(self, *args ): return self._GET(*args) def _GET(self, name): web.header("Content-Type", "text/html; charset=utf-8") web.output( self.tpl(name.replace("/","_")+".html", controller=self) )