Skip to content

Commit

Permalink
Merge branch 'master' into sprint-branch
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed May 18, 2013
2 parents 0c435cc + 9962c10 commit adb5999
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGES
Expand Up @@ -17,6 +17,7 @@ Version 2.7
to be consumed into a list.
- Python requirement changed: 2.6, 2.7 or >= 3.3 are required now,
supported by same source code, using the "six" compatibility library.
- Allow `contextfunction` and other decorators to be applied to `__call__`.

Version 2.6
-----------
Expand Down
2 changes: 1 addition & 1 deletion docs/tricks.rst
Expand Up @@ -75,7 +75,7 @@ sense to defined a default for that variable::
<ul id="navigation">
{% for href, id, caption in navigation_bar %}
<li{% if id == active_page %} class="active"{% endif
%}><a href="{{ href|e }}">{{ caption|e }}</a>/li>
%}><a href="{{ href|e }}">{{ caption|e }}</a></li>
{% endfor %}
</ul>
...
Expand Down
11 changes: 11 additions & 0 deletions jinja2/runtime.py
Expand Up @@ -176,6 +176,17 @@ def call(__self, __obj, *args, **kwargs):
"""
if __debug__:
__traceback_hide__ = True

# Allow callable classes to take a context
if hasattr(__obj, '__call__'):
fn = __obj.__call__
for fn_type in ('contextfunction',
'evalcontextfunction',
'environmentfunction'):
if hasattr(fn, fn_type):
__obj = fn
break

if isinstance(__obj, _context_function_types):
if getattr(__obj, 'contextfunction', 0):
args = (__self,) + args
Expand Down
14 changes: 13 additions & 1 deletion jinja2/testsuite/regression.py
Expand Up @@ -247,7 +247,19 @@ def test_correct_prefix_loader_name(self):
assert e.name == 'foo/bar.html'
else:
assert False, 'expected error here'


def test_contextfunction_callable_classes(self):
from jinja2.utils import contextfunction
class CallableClass(object):
@contextfunction
def __call__(self, ctx):
return ctx.resolve('hello')

tpl = Template("""{{ callableclass() }}""")
output = tpl.render(callableclass = CallableClass(), hello = 'TEST')
expected = 'TEST'

self.assert_equal(output, expected)

def suite():
suite = unittest.TestSuite()
Expand Down

0 comments on commit adb5999

Please sign in to comment.