David Wolever  committed ffb6e51

Adding a light monkeypatch.

  • Participants
  • Parent commits 1913b2c
  • Branches default

Comments (0)

Files changed (4)

+syntax: glob
       os.path.join(PROJECT_ROOT, "templates", "mako"),
-  # If MAKO_TEMPLATE_OPTS are not provided, the input_encoding and
-  # default_filters below will be used.
+  # If MAKO_TEMPLATE_OPTS are not provided, the defaults below will be used:
+      # Default values:
       'input_encoding': 'utf-8',
       'default_filters': ['h'],
+      'enable_loop': True,
+      # Other values to consider:
       'cache_impl': 'djmakocache',
+      'imports': [
+          'from myapp.templatetags import some_tag',
+      ],
       'module_directory': os.path.join(
           os.environ.get('LOGNAME', 'someuser'),
+    # By default, djmako will apply a few monkeypatches which will make Mako
+    # play more nicely with Django. Currently:
+    #   - Django's SafeData will have an ``__html__`` method injected so it will
+    #     also be considered safe by ``markupsafe``.
 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

File djmako/

 from .loader import MakoLoader, app_template_dirs
+from django.conf import settings
+if getattr(settings, "DJMAKO_MONKEYPATCH", True):
+    from .monkeypatches import patch_all
+    patch_all()
     import djmako.cache_plugin
 except ImportError:

File djmako/

+def patch_safe_data():
+    """ Patches Django's ``SafeData`` class so that it will also be considered
+        safe by ``markupsafe``. """
+    from django.utils.safestring import SafeData
+    SafeData.__html__ = lambda self: self
+def patch_all():
+    patch_safe_data()