Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

merge extensions settings

new settings variable `jinja2.extensions`
  • Loading branch information...
commit 2fb4fe4d6f7c4492ebee2be07cc1016c4a299407 1 parent 536a2f6
@aodag aodag authored
View
5 pyramid_jinja2/__init__.py
@@ -78,6 +78,7 @@ def renderer_factory(info):
directories = settings.get('jinja2.directories')
input_encoding = settings.get('jinja2.input_encoding', 'utf-8')
autoescape = settings.get('jinja2.autoescape', True)
+ extensions = settings.get('jinja2.extensions', '')
if directories is None:
raise ConfigurationError(
'Jinja2 template used without a ``jinja2.directories`` setting')
@@ -86,8 +87,10 @@ def renderer_factory(info):
loader = FileSystemLoader(directories,
encoding=input_encoding)
autoescape = asbool(autoescape)
+ extensions = [e for e in (e.strip() for e in extensions.splitlines()) if e]
environment = Environment(loader=loader, auto_reload=reload_templates,
- autoescape=True)
+ autoescape=True,
+ extensions=extensions)
filterreg = _get_filter_registry()
filterreg.install(environment)
registry.registerUtility(environment, IJinja2Environment)
View
7 pyramid_jinja2/tests/extensions.py
@@ -0,0 +1,7 @@
+from jinja2 import nodes
+from jinja2.ext import Extension
+
+class TestExtension(Extension):
+ tags = set(['test_ext'])
+ def parse(self, parser):
+ return nodes.Const("This is test extension")
View
22 pyramid_jinja2/tests/test_it.py
@@ -106,6 +106,28 @@ def dummy_filter(value):
environ = self.config.registry.getUtility(IJinja2Environment)
self.assertEqual(environ.filters['dummy'], dummy_filter)
+ def test_with_extension(self):
+ from pyramid_jinja2 import IJinja2Environment
+ settings = {'jinja2.directories':self.templates_dir,
+ 'jinja2.extensions':"""
+ pyramid_jinja2.tests.extensions.TestExtension
+ """}
+ info = DummyRendererInfo({
+ 'name':'helloworld.jinja2',
+ 'package':None,
+ 'registry':self.config.registry,
+ 'settings':settings,
+ })
+ renderer = self._callFUT(info)
+ environ = self.config.registry.getUtility(IJinja2Environment)
+ self.assertEqual(environ.loader.searchpath, [self.templates_dir])
+ self.assertEqual(renderer.info, info)
+ self.assertEqual(renderer.environment, environ)
+ import pyramid_jinja2.tests.extensions
+ self.assertTrue(isinstance(environ.extensions['pyramid_jinja2.tests.extensions.TestExtension'],
+ pyramid_jinja2.tests.extensions.TestExtension))
+
+
class Jinja2TemplateRendererTests(Base, unittest.TestCase):
def _getTargetClass(self):
from pyramid_jinja2 import Jinja2TemplateRenderer
Please sign in to comment.
Something went wrong with that request. Please try again.