Added a django template loader that can return Mako templates. Started pulling in new wiki.
This commit is contained in:
63
common/lib/mitxmako/mitxmako/makoloader.py
Normal file
63
common/lib/mitxmako/mitxmako/makoloader.py
Normal file
@@ -0,0 +1,63 @@
|
||||
|
||||
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
|
||||
|
||||
class MakoLoader(object):
|
||||
"""
|
||||
This is a Django loader object which will load the template as a
|
||||
Mako template if the first line is "## mako". It is based off BaseLoader
|
||||
in django.template.loader.
|
||||
"""
|
||||
|
||||
is_usable = False
|
||||
|
||||
def __init__(self, base_loader):
|
||||
# base_loader is an instance of a BaseLoader subclass
|
||||
self.base_loader = base_loader
|
||||
|
||||
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.base_loader.load_template_source(template_name, template_dirs)
|
||||
|
||||
if source.startswith("## mako\n"):
|
||||
# This is a mako template
|
||||
template = Template(text=source, 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)
|
||||
try:
|
||||
template = get_template_from_string(source, origin, template_name)
|
||||
return template, None
|
||||
except TemplateDoesNotExist:
|
||||
# If compiling the template we found raises TemplateDoesNotExist, back off to
|
||||
# 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
|
||||
|
||||
def load_template_source(self):
|
||||
# Just having this makes the template load as an instance, instead of a class.
|
||||
raise NotImplementedError
|
||||
|
||||
def reset(self):
|
||||
self.base_loader.reset()
|
||||
|
||||
|
||||
class MakoFilesystemLoader(MakoLoader):
|
||||
is_usable = True
|
||||
|
||||
def __init__(self):
|
||||
MakoLoader.__init__(self, FilesystemLoader())
|
||||
|
||||
class MakoAppDirectoriesLoader(MakoLoader):
|
||||
is_usable = True
|
||||
|
||||
def __init__(self):
|
||||
MakoLoader.__init__(self, AppDirectoriesLoader())
|
||||
@@ -12,18 +12,47 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
from django.conf import settings
|
||||
from mako.template import Template as MakoTemplate
|
||||
|
||||
from . import middleware
|
||||
from mitxmako import middleware
|
||||
|
||||
django_variables = ['lookup', 'template_dirs', 'output_encoding',
|
||||
django_variables = ['lookup', 'output_encoding',
|
||||
'module_directory', 'encoding_errors']
|
||||
|
||||
|
||||
class Template(MakoTemplate):
|
||||
"""
|
||||
This bridges the gap between a Mako template and a djano template. It can
|
||||
be rendered like it is a django template because the arguments are transformed
|
||||
in a way that MakoTemplate can understand.
|
||||
"""
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
"""Overrides base __init__ to provide django variable overrides"""
|
||||
if not kwargs.get('no_django', False):
|
||||
overrides = dict([(k, getattr(middleware, k, None),) for k in django_variables])
|
||||
overrides['lookup'] = overrides['lookup']['main']
|
||||
kwargs.update(overrides)
|
||||
super(Template, self).__init__(*args, **kwargs)
|
||||
|
||||
|
||||
def render(self, context_instance):
|
||||
"""
|
||||
This takes a render call with a context (from Django) and translates
|
||||
it to a render call on the mako template.
|
||||
"""
|
||||
# collapse context_instance to a single dictionary for mako
|
||||
context_dictionary = {}
|
||||
|
||||
# In various testing contexts, there might not be a current request context.
|
||||
if middleware.requestcontext is not None:
|
||||
for d in middleware.requestcontext:
|
||||
context_dictionary.update(d)
|
||||
for d in context_instance:
|
||||
context_dictionary.update(d)
|
||||
context_dictionary['settings'] = settings
|
||||
context_dictionary['MITX_ROOT_URL'] = settings.MITX_ROOT_URL
|
||||
|
||||
return super(Template, self).render(**context_dictionary)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user