diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py index 6adbac6f5c2d6b9d1bd12ae2e2b6b3adaa09f909..1656a53f83f1ce8d75f6866432d64df754c615b7 100644 --- a/lib/mako/codegen.py +++ b/lib/mako/codegen.py @@ -238,7 +238,7 @@ class _GenerateRenderMethod(object): self.printer.writeline("return ''") self.printer.writeline(None) self.printer.writeline("context.push(**{%s})" % - (','.join(["%s:%s" % (repr(x), x) for x in export] + ["'callargs':runtime.AttrDict(**{%s})" % ','.join(["%s:%s" % (repr(x), x) for x in export])]) ) + (','.join(["%s:%s" % (repr(x), x) for x in export]) ) ) self.printer.writeline("context.write(unicode(%s))" % node.attributes['expr']) self.printer.writeline("context.pop()") diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py index a5fd1c36f0606ffc497e1eca05c7a26714602710..16ee62796786c8752f6acc076c45a908e60e44e2 100644 --- a/lib/mako/runtime.py +++ b/lib/mako/runtime.py @@ -16,16 +16,16 @@ class Context(object): self.stack = [data] # the Template instance currently rendering with this context. self.with_template = template + class AttrFacade(object): + def __getattr__(s, key): + return self.stack[-1][key] + data.setdefault('args', AttrFacade()) def __getitem__(self, key): return self.stack[-1][key] - def __setitem__(self, key, value): - self.stack[-1][key] = value def get(self, key, default=None): return self.stack[-1].get(key, default) def write(self, string): self.buffer.write(string) - def update(self, **args): - self.stack[-1].update(args) def push(self, **args): x = self.stack[-1].copy() x.update(args) @@ -34,7 +34,7 @@ class Context(object): self.stack.pop() def locals_(self, d): c = Context(self.with_template, self.buffer, **self.stack[-1]) - c.update(**d) + c.stack[-1].update(**d) return c class Undefined(object): @@ -43,20 +43,6 @@ class Undefined(object): raise NameError("Undefined") UNDEFINED = Undefined() -class AttrDict(object): - """dictionary facade providing getattr access""" - def __init__(self, **data): - self.data = data - def __getattr__(self, key): - return self.data[key] - def __getitem__(self, key): - return self.data[key] - def __setitem__(self, key, value): - self.data[key] = value - def __iter__(self): - return self.data.keys() - def keys(self): - return self.data.keys() class Namespace(object): """provides access to collections of rendering methods, which can be local, from other templates, or from imported modules""" diff --git a/test/call.py b/test/call.py index 2b74ab47e0beb6abc6ad0fdf8930462d13958003..0354576c732264215b13a27f7ed24abe4d3f5e97 100644 --- a/test/call.py +++ b/test/call.py @@ -31,7 +31,7 @@ class CallTest(unittest.TestCase): </%component> <%component name="foo"> - foo calling comp1: ${callargs.comp1()} + foo calling comp1: ${args.comp1()} foo calling body: ${body()} </%component> diff --git a/test/component.py b/test/component.py index b255d9b81dc250ac5a9825353724d36f20d5f11d..3b0593f5b90cd570df400b73fe3f2bd739b9c707 100644 --- a/test/component.py +++ b/test/component.py @@ -193,7 +193,6 @@ class ScopeTest(unittest.TestCase): <%component name="b"> <% x = 10 - context['x'] = x %> b. x is ${x}. ${a()} @@ -203,7 +202,7 @@ class ScopeTest(unittest.TestCase): </%component> ${enclosing()} """) - assert flatten_result(t.render(x=5)) == "b. x is 10. a: x is 10" + assert flatten_result(t.render(x=5)) == "b. x is 10. a: x is 5" def test_scope_nine(self): """test that 'enclosing scope' doesnt get exported to other templates"""