summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGES.txt7
-rw-r--r--docs/narr/environment.rst29
-rw-r--r--docs/whatsnew-1.1.rst4
-rw-r--r--pyramid/mako_templating.py8
-rw-r--r--pyramid/tests/test_mako_templating.py15
5 files changed, 57 insertions, 6 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index b95211d09..81004b00e 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,6 +1,13 @@
Next release
============
+Features
+--------
+
+- Added ``mako.preprocessor`` config file parameter; allows for a Mako
+ preprocessor to be specified as a Python callable or Python dotted name.
+ See https://github.com/Pylons/pyramid/pull/183 for rationale.
+
Bug fixes
---------
diff --git a/docs/narr/environment.rst b/docs/narr/environment.rst
index 3b938c09c..a57b316e1 100644
--- a/docs/narr/environment.rst
+++ b/docs/narr/environment.rst
@@ -227,11 +227,11 @@ should be changed accordingly.
Mako Error Handler
++++++++++++++++++
-Python callable which is called whenever Mako compile or runtime exceptions
-occur. The callable is passed the current context as well as the exception. If
-the callable returns True, the exception is considered to be handled, else it
-is re-raised after the function completes. Is used to provide custom
-error-rendering functions.
+A callable (or a :term:`dotted Python name` which names a callable) which is
+called whenever Mako compile or runtime exceptions occur. The callable is
+passed the current context as well as the exception. If the callable returns
+True, the exception is considered to be handled, else it is re-raised after
+the function completes. Is used to provide custom error-rendering functions.
+-----------------------------+
| Config File Setting Name |
@@ -290,6 +290,25 @@ default, this is ``false``.
| |
+-----------------------------+
+Mako Preprocessor
++++++++++++++++++
+
+A callable (or a :term:`dotted Python name` which names a callable) which is
+called to preprocess the source before the template is called. The callable
+will be passed the full template source before it is parsed. The return
+result of the callable will be used as the template source code.
+
+.. note:: This feature is new in Pyramid 1.1.
+
++-----------------------------+
+| Config File Setting Name |
++=============================+
+| ``mako.preprocessor`` |
+| |
+| |
+| |
++-----------------------------+
+
Examples
--------
diff --git a/docs/whatsnew-1.1.rst b/docs/whatsnew-1.1.rst
index 180380608..4d7567886 100644
--- a/docs/whatsnew-1.1.rst
+++ b/docs/whatsnew-1.1.rst
@@ -165,6 +165,10 @@ Minor Feature Additions
- :class:`pyramid.exceptions.Forbidden` is now just an alias for
:class:`pyramid.httpexceptions.HTTPForbidden`.
+- Added ``mako.preprocessor`` config file parameter; allows for a Mako
+ preprocessor to be specified as a Python callable or Python dotted name.
+ See https://github.com/Pylons/pyramid/pull/183 for rationale.
+
Backwards Incompatibilities
---------------------------
diff --git a/pyramid/mako_templating.py b/pyramid/mako_templating.py
index fea8066d4..3055e2cfb 100644
--- a/pyramid/mako_templating.py
+++ b/pyramid/mako_templating.py
@@ -69,6 +69,7 @@ def renderer_factory(info):
default_filters = settings.get('mako.default_filters', 'h')
imports = settings.get('mako.imports', None)
strict_undefined = settings.get('mako.strict_undefined', 'false')
+ preprocessor = settings.get('mako.preprocessor', None)
if directories is None:
raise ConfigurationError(
'Mako template used without a ``mako.directories`` setting')
@@ -87,6 +88,10 @@ def renderer_factory(info):
if not hasattr(imports, '__iter__'):
imports = filter(None, imports.splitlines())
strict_undefined = asbool(strict_undefined)
+ if preprocessor is not None:
+ dotted = DottedNameResolver(info.package)
+ preprocessor = dotted.maybe_resolve(preprocessor)
+
lookup = PkgResourceTemplateLookup(directories=directories,
module_directory=module_directory,
@@ -95,7 +100,8 @@ def renderer_factory(info):
default_filters=default_filters,
imports=imports,
filesystem_checks=reload_templates,
- strict_undefined=strict_undefined)
+ strict_undefined=strict_undefined,
+ preprocessor=preprocessor)
registry_lock.acquire()
try:
registry.registerUtility(lookup, IMakoLookup)
diff --git a/pyramid/tests/test_mako_templating.py b/pyramid/tests/test_mako_templating.py
index 6b2adbe09..c63895216 100644
--- a/pyramid/tests/test_mako_templating.py
+++ b/pyramid/tests/test_mako_templating.py
@@ -139,6 +139,21 @@ class Test_renderer_factory(Base, unittest.TestCase):
lookup = self.config.registry.getUtility(IMakoLookup)
self.assertEqual(lookup.template_args['error_handler'], pyramid.tests)
+ def test_with_preprocessor(self):
+ from pyramid.mako_templating import IMakoLookup
+ settings = {'mako.directories':self.templates_dir,
+ 'mako.preprocessor':'pyramid.tests'}
+ import pyramid.tests
+ info = DummyRendererInfo({
+ 'name':'helloworld.mak',
+ 'package':None,
+ 'registry':self.config.registry,
+ 'settings':settings,
+ })
+ self._callFUT(info)
+ lookup = self.config.registry.getUtility(IMakoLookup)
+ self.assertEqual(lookup.template_args['preprocessor'], pyramid.tests)
+
def test_with_default_filters(self):
from pyramid.mako_templating import IMakoLookup
settings = {'mako.directories':self.templates_dir,