Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

adding settings for extensions and filters #7

Merged
4 commits merged into from

2 participants

@aodag

new settings variables jinja2.filters and jinja2.extensions.

aodag added some commits
@aodag aodag Squashed commit of the following:
commit d274b822186eac40dcbddf8970d1ff8f86cc4de9
Author: Atsushi Odagiri <aodagx@gmail.com>
Date:   Thu Nov 25 13:17:05 2010 +0900

    return filter func to use decolator.

commit 219419cd81d14aaaef5741dae2c162eec1aab6e2
Author: Atsushi Odagiri <aodagx@gmail.com>
Date:   Wed Nov 24 17:36:45 2010 +0900

    filter registry for jinja2
536a2f6
@aodag aodag merge extensions settings
new settings variable `jinja2.extensions`
2fb4fe4
@aodag aodag add `jinja2.filter` settings variable to register filters to environm…
…ent.
2b5eeff
@aodag aodag trivial change 2c55ae2
@mcdonc
Owner

This work has been merged (in a slightly modified fashion) to the trunk. Thanks!

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Nov 25, 2010
  1. @aodag

    Squashed commit of the following:

    aodag authored
    commit d274b822186eac40dcbddf8970d1ff8f86cc4de9
    Author: Atsushi Odagiri <aodagx@gmail.com>
    Date:   Thu Nov 25 13:17:05 2010 +0900
    
        return filter func to use decolator.
    
    commit 219419cd81d14aaaef5741dae2c162eec1aab6e2
    Author: Atsushi Odagiri <aodagx@gmail.com>
    Date:   Wed Nov 24 17:36:45 2010 +0900
    
        filter registry for jinja2
  2. @aodag

    merge extensions settings

    aodag authored
    new settings variable `jinja2.extensions`
  3. @aodag
  4. @aodag

    trivial change

    aodag authored
This page is out of date. Refresh to see the latest.
View
21 pyramid_jinja2/__init__.py
@@ -2,6 +2,7 @@
from zope.interface import Interface
from jinja2.loaders import FileSystemLoader
+from jinja2.utils import import_string
from jinja2 import Environment
from pyramid.interfaces import ITemplateRenderer
@@ -12,6 +13,7 @@
class IJinja2Environment(Interface):
pass
+
def asbool(obj):
if isinstance(obj, (str, unicode)):
obj = obj.strip().lower()
@@ -24,6 +26,16 @@ def asbool(obj):
"String is not true/false: %r" % obj)
return bool(obj)
+def parse_filters(filters):
+ if isinstance(filters, basestring):
+ return dict([f.split('=', 1) for f in (f.strip() for f in filters.splitlines()) if f])
+ elif isinstance(filters, list) or isinstance(filters, tuple):
+ return dict(filters)
+ elif isinstance(filters, dict):
+ return filters
+ else:
+ raise ValueError("jinja2.filters is not formated string or list or dict.")
+
def renderer_factory(info):
registry = info.registry
settings = info.settings
@@ -33,6 +45,8 @@ 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', '')
+ filters = settings.get('jinja2.filters', '')
if directories is None:
raise ConfigurationError(
'Jinja2 template used without a ``jinja2.directories`` setting')
@@ -41,8 +55,13 @@ 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]
+ filters = parse_filters(filters)
environment = Environment(loader=loader, auto_reload=reload_templates,
- autoescape=True)
+ autoescape=True,
+ extensions=extensions)
+ for name, filter in filters.iteritems():
+ environment.filters[name] = import_string(filter) if isinstance(filter, basestring) else filter
registry.registerUtility(environment, IJinja2Environment)
return Jinja2TemplateRenderer(info, environment)
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
84 pyramid_jinja2/tests/test_it.py
@@ -2,6 +2,9 @@
import unittest
+def dummy_filter(value):
+ return 'hoge'
+
class Test_asbool(unittest.TestCase):
def _callFUT(self, value):
from pyramid_jinja2 import asbool
@@ -16,6 +19,31 @@ def test_str_false(self):
def test_str_unrecognized(self):
self.assertRaises(ValueError, self._callFUT, '123')
+class Test_parse_filters(unittest.TestCase):
+ def _callFUT(self, value):
+ from pyramid_jinja2 import parse_filters
+ return parse_filters(value)
+
+ def test_parse_singile_line(self):
+ self.assertEqual(self._callFUT('dummy=dummy'), {'dummy':'dummy'})
+
+ def test_parse_multi_line(self):
+ self.assertEqual(self._callFUT("""\
+ dummy=dummy
+ dummy2=dummy"""),
+ {'dummy':'dummy', 'dummy2':'dummy'})
+
+ def test_parse_list(self):
+ self.assertEqual(self._callFUT(
+ [('dummy', 'dummy'),
+ ('dummy2', 'dummy')]),
+ {'dummy':'dummy', 'dummy2':'dummy'})
+
+ def test_parse_dict(self):
+ self.assertEqual(self._callFUT(
+ {'dummy': 'dummy',
+ 'dummy2': 'dummy'}),
+ {'dummy':'dummy', 'dummy2':'dummy'})
class Base(object):
def setUp(self):
@@ -87,6 +115,62 @@ def test_with_environ(self):
self.assertEqual(renderer.environment, environ)
self.assertEqual(renderer.info, info)
+ def test_with_filters_object(self):
+ from pyramid_jinja2 import IJinja2Environment
+
+ def dummy_filter(value):
+ return 'hoge'
+
+ settings = {'jinja2.directories':self.templates_dir,
+ 'jinja2.filters':{'dummy':dummy_filter}}
+ 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.filters['dummy'], dummy_filter)
+
+ def test_with_filters_string(self):
+ from pyramid_jinja2 import IJinja2Environment
+
+
+ settings = {'jinja2.directories':self.templates_dir,
+ 'jinja2.filters':'dummy=pyramid_jinja2.tests.test_it:dummy_filter'}
+ 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.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
Something went wrong with that request. Please try again.