diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-03-08 01:27:21 +0000 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2007-03-08 01:27:21 +0000 |
commit | a7889d1be5516f1ef6b46cc0dc54b01f39f07a33 (patch) | |
tree | 1d894b78906a48ab2b5cc7bc85360467840dfce9 | |
parent | e7193c382cc9b8d73d68a446cace36a7661b7714 (diff) | |
download | external_python_mako-a7889d1be5516f1ef6b46cc0dc54b01f39f07a33.tar.gz external_python_mako-a7889d1be5516f1ef6b46cc0dc54b01f39f07a33.tar.bz2 external_python_mako-a7889d1be5516f1ef6b46cc0dc54b01f39f07a33.zip |
- <%include> has an "args" attribute that can pass arguments to the called
template (keyword arguments only, must be declared in that page's <%page> tag.)
-rw-r--r-- | CHANGES | 2 | ||||
-rw-r--r-- | lib/mako/codegen.py | 8 | ||||
-rw-r--r-- | lib/mako/parsetree.py | 7 | ||||
-rw-r--r-- | lib/mako/runtime.py | 9 | ||||
-rw-r--r-- | test/template.py | 38 |
5 files changed, 55 insertions, 9 deletions
@@ -7,6 +7,8 @@ pre-process the ".." tokens before checking the filesystem - fixed/improved "caller" semantics so that undefined caller is "UNDEFINED", propigates __nonzero__ method so it evaulates to False if not present, True otherwise. this way you can say % if caller:\n ${caller.body()}\n% endif +- <%include> has an "args" attribute that can pass arguments to the called +template (keyword arguments only, must be declared in that page's <%page> tag.) 0.1.3 - ***Small Syntax Change*** - the single line comment character is now diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py index 0621e27..9f05fb5 100644 --- a/lib/mako/codegen.py +++ b/lib/mako/codegen.py @@ -475,8 +475,12 @@ class _GenerateRenderMethod(object): def visitIncludeTag(self, node): self.write_source_comment(node) - self.printer.writeline("runtime._include_file(context, %s, _template_uri)" % (node.parsed_attributes['file'])) - + args = node.attributes.get('args') + if args: + self.printer.writeline("runtime._include_file(context, %s, _template_uri, %s)" % (node.parsed_attributes['file'], args)) + else: + self.printer.writeline("runtime._include_file(context, %s, _template_uri)" % (node.parsed_attributes['file'])) + def visitNamespaceTag(self, node): pass diff --git a/lib/mako/parsetree.py b/lib/mako/parsetree.py index 5aabeb3..7e5a5ae 100644 --- a/lib/mako/parsetree.py +++ b/lib/mako/parsetree.py @@ -219,7 +219,12 @@ class Tag(Node): class IncludeTag(Tag): __keyword__ = 'include' def __init__(self, keyword, attributes, **kwargs): - super(IncludeTag, self).__init__(keyword, attributes, ('file', 'import'), (), ('file',), **kwargs) + super(IncludeTag, self).__init__(keyword, attributes, ('file', 'import', 'args'), (), ('file',), **kwargs) + self.page_args = ast.PythonCode("foo(%s)" % attributes.get('args', ''), self.lineno, self.pos, self.filename) + def declared_identifiers(self): + return [] + def undeclared_identifiers(self): + return self.page_args.undeclared_identifiers class NamespaceTag(Tag): __keyword__ = 'namespace' diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py index 5f4e419..fa9403e 100644 --- a/lib/mako/runtime.py +++ b/lib/mako/runtime.py @@ -221,11 +221,11 @@ def capture(context, callable_, *args, **kwargs): buf = context.pop_buffer() return buf.getvalue() -def _include_file(context, uri, calling_uri): +def _include_file(context, uri, calling_uri, **kwargs): """locate the template from the given uri and include it in the current output.""" template = _lookup_template(context, uri, calling_uri) (callable_, ctx) = _populate_self_namespace(context._clean_inheritance_tokens(), template) - callable_(ctx, **_kwargs_for_callable(callable_, context._data)) + callable_(ctx, **_kwargs_for_callable(callable_, context._data, **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 @@ -284,12 +284,11 @@ def _render(template, callable_, args, data, as_unicode=False): _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, **kwargs): argspec = inspect.getargspec(callable_) namedargs = argspec[0] + [v for v in argspec[1:3] if v is not None] for arg in namedargs: - if arg != 'context' and arg in data: + if arg != 'context' and arg in data and arg not in kwargs: kwargs[arg] = data[arg] return kwargs diff --git a/test/template.py b/test/template.py index 871b34f..658651d 100644 --- a/test/template.py +++ b/test/template.py @@ -148,8 +148,44 @@ class PageArgsTest(unittest.TestCase): assert flatten_result(template.render(id="im the id")) == "this is page, id is im the id" +class IncludeTest(unittest.TestCase): + def test_basic(self): + lookup = TemplateLookup() + lookup.put_string("a", """ + this is a + <%include file="b" args="a=3,b=4,c=5"/> + """) + lookup.put_string("b", """ + <%page args="a,b,c"/> + this is b. ${a}, ${b}, ${c} + """) + assert flatten_result(lookup.get_template("a").render()) == "this is a this is b. 3, 4, 5" + + def test_localargs(self): + lookup = TemplateLookup() + lookup.put_string("a", """ + this is a + <%include file="b" args="a=a,b=b,c=5"/> + """) + lookup.put_string("b", """ + <%page args="a,b,c"/> + this is b. ${a}, ${b}, ${c} + """) + assert flatten_result(lookup.get_template("a").render(a=7,b=8)) == "this is a this is b. 7, 8, 5" + + def test_viakwargs(self): + lookup = TemplateLookup() + lookup.put_string("a", """ + this is a + <%include file="b" args="c=5, **context.kwargs"/> + """) + lookup.put_string("b", """ + <%page args="a,b,c"/> + this is b. ${a}, ${b}, ${c} + """) + assert flatten_result(lookup.get_template("a").render(a=7,b=8)) == "this is a this is b. 7, 8, 5" + - class ControlTest(unittest.TestCase): def test_control(self): t = Template(""" |