Skip to content
Snippets Groups Projects
Commit aa84c7d5 authored by Mike Bayer's avatar Mike Bayer
Browse files

adjustments to context to support 'args'

parent 62606570
No related branches found
No related tags found
No related merge requests found
......@@ -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()")
......
......@@ -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"""
......
......@@ -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>
......
......@@ -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"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment