Skip to content
Browse files

add a template_loader application setting for using custom template l…

…oaders. 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.
  • Loading branch information...
1 parent 8359c05 commit 03308b1a7dad63d5ff12155d097fb6ee110a4cf3 Dolapo Falola committed with Ben Darnell Mar 17, 2010
Showing with 14 additions and 4 deletions.
  1. +8 −1 tornado/template.py
  2. +6 −3 tornado/web.py
View
9 tornado/template.py
@@ -169,7 +169,10 @@ def __init__(self, root_directory):
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 @@ def load(self, name, parent_path=None):
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")
View
9 tornado/web.py
@@ -417,8 +417,9 @@ def render_string(self, template_name, **kwargs):
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 @@ def __call__(self, request):
# 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)

0 comments on commit 03308b1

Please sign in to comment.
Something went wrong with that request. Please try again.