The django/mako template loader now caches mako templates by loading them from file.
This commit is contained in:
@@ -1,10 +1,16 @@
|
||||
import logging
|
||||
|
||||
from django.conf import settings
|
||||
from django.template.base import TemplateDoesNotExist
|
||||
from django.template.loader import make_origin, get_template_from_string
|
||||
from django.template.loaders.filesystem import Loader as FilesystemLoader
|
||||
from django.template.loaders.app_directories import Loader as AppDirectoriesLoader
|
||||
|
||||
from mitxmako.template import Template
|
||||
import mitxmako.middleware
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
class MakoLoader(object):
|
||||
"""
|
||||
@@ -18,20 +24,29 @@ class MakoLoader(object):
|
||||
def __init__(self, base_loader):
|
||||
# base_loader is an instance of a BaseLoader subclass
|
||||
self.base_loader = base_loader
|
||||
|
||||
|
||||
module_directory = getattr(settings, 'MAKO_MODULE_DIR', None)
|
||||
|
||||
if module_directory is None:
|
||||
log.warning("For more caching of mako templates, set the MAKO_MODULE_DIR in settings!")
|
||||
module_directory = tempfile.mkdtemp()
|
||||
|
||||
self.module_directory = module_directory
|
||||
|
||||
|
||||
def __call__(self, template_name, template_dirs=None):
|
||||
return self.load_template(template_name, template_dirs)
|
||||
|
||||
def load_template(self, template_name, template_dirs=None):
|
||||
source, display_name = self.load_template_source(template_name, template_dirs)
|
||||
source, file_path = self.load_template_source(template_name, template_dirs)
|
||||
|
||||
if source.startswith("## mako\n"):
|
||||
# This is a mako template
|
||||
template = Template(text=source, uri=template_name)
|
||||
template = Template(filename=file_path, module_directory=self.module_directory, uri=template_name)
|
||||
return template, None
|
||||
else:
|
||||
# This is a regular template
|
||||
origin = make_origin(display_name, self.load_template_source, template_name, template_dirs)
|
||||
origin = make_origin(file_path, self.load_template_source, template_name, template_dirs)
|
||||
try:
|
||||
template = get_template_from_string(source, origin, template_name)
|
||||
return template, None
|
||||
@@ -40,7 +55,7 @@ class MakoLoader(object):
|
||||
# returning the source and display name for the template we were asked to load.
|
||||
# This allows for correct identification (later) of the actual template that does
|
||||
# not exist.
|
||||
return source, display_name
|
||||
return source, file_path
|
||||
|
||||
def load_template_source(self, template_name, template_dirs=None):
|
||||
# Just having this makes the template load as an instance, instead of a class.
|
||||
|
||||
@@ -17,8 +17,7 @@ from mako.template import Template as MakoTemplate
|
||||
|
||||
from mitxmako import middleware
|
||||
|
||||
django_variables = ['lookup', 'output_encoding',
|
||||
'module_directory', 'encoding_errors']
|
||||
django_variables = ['lookup', 'output_encoding', 'encoding_errors']
|
||||
|
||||
# TODO: We should make this a Django Template subclass that simply has the MakoTemplate inside of it? (Intead of inheriting from MakoTemplate)
|
||||
class Template(MakoTemplate):
|
||||
|
||||
Reference in New Issue
Block a user