Skip to content
Myr

Merge pull request #200 from Naddiseo/callable_class_contextfunction

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Callable class contextfunction, closes #180

authored by Armin Ronacher · committed by Armin Ronacher · parents 6d11d17, 71fc228 · Browse files · 2 files+24 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−1 removed

  • other:+24 added−1 removed

2 files+24 added−1 removed

modified:jinja2/testsuite/regression.py+13 added-1 removed
244 unmodified lines
245 except TemplateNotFound, e:246 assert e.name == 'foo/bar.html'247 else:248 assert False, 'expected error here'249 250 def test_contextfunction_callable_classes(self):251 from jinja2.utils import contextfunction252 class CallableClass(object):253 @contextfunction254 def __call__(self, ctx):255 return ctx.resolve('hello')256 257 tpl = Template("""{{ callableclass() }}""")258 output = tpl.render(callableclass = CallableClass(), hello = 'TEST')259 expected = 'TEST'260 261 self.assert_equal(output, expected)262250263def suite():264 suite = unittest.TestSuite()265 suite.addTest(unittest.makeSuite(CornerTestCase))266 suite.addTest(unittest.makeSuite(BugTestCase))
1 unmodified line
modified:jinja2/runtime.py+11 added
169 unmodified lines
170 :func:`environmentfunction`.171 """172 if __debug__:173 __traceback_hide__ = True174 175 # Allow callable classes to take a context176 if hasattr(__obj, '__call__'):177 fn = __obj.__call__178 for fn_type in ('contextfunction', 179 'evalcontextfunction', 180 'environmentfunction'):181 if hasattr(fn, fn_type):182 __obj = fn183 break;184 185 if isinstance(__obj, _context_function_types):186 if getattr(__obj, 'contextfunction', 0):187 args = (__self,) + args188 elif getattr(__obj, 'evalcontextfunction', 0):
384 unmodified lines