Skip to content

Commit

Permalink
- Calling a def from the top, via
Browse files Browse the repository at this point in the history
  template.get_def(...).render() now checks the
  argument signature the same way as it did in
  0.2.5, so that TypeError is not raised.
  reopen of [ticket:116]
  • Loading branch information
zzzeek committed Mar 11, 2010
1 parent 838a8d6 commit 2bf553e
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
0.3.2
- Calling a def from the top, via
template.get_def(...).render() now checks the
argument signature the same way as it did in
0.2.5, so that TypeError is not raised.
reopen of [ticket:116]


0.3.1
- Fixed incorrect dir name in setup.py
[ticket:129]
Expand Down
2 changes: 1 addition & 1 deletion mako/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
# the MIT License: http://www.opensource.org/licenses/mit-license.php


__version__ = '0.3.1'
__version__ = '0.3.2'

21 changes: 18 additions & 3 deletions mako/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def _include_file(context, uri, calling_uri, **kwargs):

template = _lookup_template(context, uri, calling_uri)
(callable_, ctx) = _populate_self_namespace(context._clean_inheritance_tokens(), template)
callable_(ctx, **_kwargs_for_callable(callable_, context._orig, **kwargs))
callable_(ctx, **_kwargs_for_include(callable_, context._orig, **kwargs))

def _inherit_from(context, uri, calling_uri):
"""called by the _inherit method in template modules to set up the inheritance chain at the start
Expand Down Expand Up @@ -399,10 +399,25 @@ def _render(template, callable_, args, data, as_unicode=False):
context = Context(buf, **data)
context._outputting_as_unicode = as_unicode
context._with_template = template
_render_context(template, callable_, context, *args, **data)

_render_context(template, callable_, context, *args, **_kwargs_for_callable(callable_, data))
return context._pop_buffer().getvalue()

def _kwargs_for_callable(callable_, data, **kwargs):
def _kwargs_for_callable(callable_, data):
argspec = inspect.getargspec(callable_)
# for normal pages, **pageargs is usually present
if argspec[2]:
return data

# for rendering defs from the top level, figure out the args
namedargs = argspec[0] + [v for v in argspec[1:3] if v is not None]
kwargs = {}
for arg in namedargs:
if arg != 'context' and arg in data and arg not in kwargs:
kwargs[arg] = data[arg]
return kwargs

def _kwargs_for_include(callable_, data, **kwargs):
argspec = inspect.getargspec(callable_)
namedargs = argspec[0] + [v for v in argspec[1:3] if v is not None]
for arg in namedargs:
Expand Down
3 changes: 3 additions & 0 deletions test/test_def.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ def test_toplevel(self):
filters=flatten_result)
self._do_test(template.get_def("body"), "this is the body", filters=flatten_result)

# test that args outside of the dict can be used
self._do_test(template.get_def("a"), "this is a",
filters=flatten_result, template_args={'q':5,'zq':'test'})

class ScopeTest(TemplateTest):
"""test scoping rules. The key is, enclosing scope always takes precedence over contextual scope."""
Expand Down

0 comments on commit 2bf553e

Please sign in to comment.