From: Dolapo Falola Date: Thu, 18 Mar 2010 01:44:16 +0000 (-0700) Subject: add a template_loader application setting for using custom template loaders. a slight... X-Git-Tag: v1.0.0~73^2~4 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=03308b1a7dad63d5ff12155d097fb6ee110a4cf3;p=thirdparty%2Ftornado.git add a template_loader application setting for using custom template loaders. a slight refactoring of TemplateLoader's load method to expose some internals. also, add a reset method to the loader that's called in debug mode to flush the template cache. --- diff --git a/tornado/template.py b/tornado/template.py index b5e94bda7..b62735ee9 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -169,7 +169,10 @@ class Loader(object): self.root = os.path.abspath(root_directory) self.templates = {} - def load(self, name, parent_path=None): + def reset(self): + self.templates = {} + + def resolve_path(self, name, parent_path=None): if parent_path and not parent_path.startswith("<") and \ not parent_path.startswith("/") and \ not name.startswith("/"): @@ -178,6 +181,10 @@ class Loader(object): relative_path = os.path.abspath(os.path.join(file_dir, name)) if relative_path.startswith(self.root): name = relative_path[len(self.root) + 1:] + return name + + def load(self, name, parent_path=None): + name = self.resolve_path(name, parent_path=parent_path) if name not in self.templates: path = os.path.join(self.root, name) f = open(path, "r") diff --git a/tornado/web.py b/tornado/web.py index d901c4f67..766a441ff 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -417,8 +417,9 @@ class RequestHandler(object): if not getattr(RequestHandler, "_templates", None): RequestHandler._templates = {} if template_path not in RequestHandler._templates: - RequestHandler._templates[template_path] = template.Loader( - template_path) + loader = self.application.settings.get("template_loader") or\ + template.Loader(template_path) + RequestHandler._templates[template_path] = loader t = RequestHandler._templates[template_path].load(template_name) args = dict( handler=self, @@ -1020,7 +1021,9 @@ class Application(object): # In debug mode, re-compile templates and reload static files on every # request so you don't need to restart to see changes if self.settings.get("debug"): - RequestHandler._templates = None + if getattr(RequestHandler, "_templates", None): + map(lambda loader: loader.reset(), + RequestHandler._templates.values()) RequestHandler._static_hashes = {} handler._execute(transforms, *args, **kwargs)