David Wolever  committed 1913b2c

More Django-esque default values for MAKO_TEMPLATE_DIRS and MAKO_TEMPLATE_OPTS

  • Participants
  • Parent commits 87a4a85

Comments (0)

Files changed (3)

 in one project.
 To use, add ``'djmako.MakoLoader'`` to ``settings.TEMPLATE_LOADERS``, and
-define settings.MAKO_TEMPLATE_DIRS (a tuple of directories in which to
-look for mako templates -- required) and ``settings.MAKO_TEMPLATE_OPTS``
-(a dictionary of other parameters to pass to Mako's ``TemplateLookup``
-class -- optional).
+optionally define ``settings.MAKO_TEMPLATE_DIRS`` (a tuple of directories in
+which to look for mako templates) and ``settings.MAKO_TEMPLATE_OPTS`` (a
+dictionary of other parameters to pass to Mako's ``TemplateLookup`` class).  If
+values are not defined, defaults similar to Django's defaults will be used.
 For instance, in ```` you might have::
-  MAKO_TEMPLATE_DIRS=(os.path.join(PROJECT_ROOT, 'templates', 'mako'),)
+  # If MAKO_TEMPLATE_DIRS are not provided, ``TEMPLATE_DIRS +
+  # app_template_dirs`` will be used. Note taht ``app_template_dirs`` is a
+  # tuple of all the app template directories, used by
+  # ``django.templates.loaders.app_directories.Loader``.
+  from djmako import app_template_dirs
+  MAKO_TEMPLATE_DIRS = TEMPLATE_DIRS + app_template_dirs + (
+      os.path.join(PROJECT_ROOT, "templates", "mako"),
+  )
-  MAKO_TEMPLATE_OPTS=dict(input_encoding='utf-8',
-                          cache_impl='djmakocache', 
-                          module_directory=os.path.join(
-      tempfile.gettempdir(),
-      os.environ.get('LOGNAME', 'someuser'),
-      'mysite',
-      PROJECT_ROOT.split('/')[-2]))
+  # If MAKO_TEMPLATE_OPTS are not provided, the input_encoding and
+  # default_filters below will be used.
+      'input_encoding': 'utf-8',
+      'default_filters': ['h'],
+      'cache_impl': 'djmakocache',
+      'module_directory': os.path.join(
+          tempfile.gettempdir(),
+          os.environ.get('LOGNAME', 'someuser'),
+          'mysite',
+          PROJECT_ROOT.split('/')[-2]
+      ),
+    }
-Note that in the above configuration, the mako loader is checked
-first, and django templates afterwards.
+Note that in the above configuration, the mako loader is checked first, and
+django templates afterwards. Because of this, the ``MakoLoader`` requires Mako
+template files to have a ``.mako`` extension, so it doesn't try to load Django
+template files.
 Cache Plugin

File djmako/

 Mako templates for django >= 1.2.
-from .loader import MakoLoader
+from .loader import MakoLoader, app_template_dirs
     import djmako.cache_plugin

File djmako/

 from django.template import TemplateDoesNotExist
 from django.template.context import Context
 from django.template.loaders.filesystem import Loader as FSLoader
+from django.template.loaders.app_directories import app_template_dirs
 from mako.exceptions import MakoException, TopLevelLookupException
 from mako.template import Template
 _lookup = None
 def get_lookup():
     global _lookup
     if _lookup is None:
-        opts = getattr(settings, 'MAKO_TEMPLATE_OPTS', {})
-        _lookup = TemplateLookup(directories=settings.MAKO_TEMPLATE_DIRS,
-                                 **opts)
+        # If values aren't explicitly specified, default to behaving more or
+        # less like Django does by default: escape text by default, load
+        # templates from app template directories.
+        opts = getattr(settings, 'MAKO_TEMPLATE_OPTS', {
+            'input_encoding': 'utf-8',
+            'default_filters': ['h'],
+        })
+        directories = getattr(settings, 'MAKO_TEMPLATE_DIRS', None)
+        if directories is None:
+            directories = settings.TEMPLATE_DIRS + app_template_dirs
+        _lookup = TemplateLookup(directories=directories, **opts)
     return _lookup
     is_usable = True
     def load_template(self, template_name, template_dirs=None):
+        if not template_name.endswith('.mako'):
+            raise TemplateDoesNotExist(
+                '%r is not a ".mako" file' % template_name)
         lookup = get_lookup()
             real_template = lookup.get_template(template_name)