From 6cb404853f88b368fadba2e7a717a70c70065a7a Mon Sep 17 00:00:00 2001 From: Mike Bayer <mike_mp@zzzcomputing.com> Date: Sun, 3 Dec 2006 00:14:30 +0000 Subject: [PATCH] dynamic inheritance --- lib/mako/codegen.py | 2 +- lib/mako/parsetree.py | 3 ++- lib/mako/runtime.py | 4 +++- test/inheritance.py | 27 +++++++++++++++++++++++++-- 4 files changed, 31 insertions(+), 5 deletions(-) diff --git a/lib/mako/codegen.py b/lib/mako/codegen.py index aaa9c47..779f27f 100644 --- a/lib/mako/codegen.py +++ b/lib/mako/codegen.py @@ -136,7 +136,7 @@ class _GenerateRenderMethod(object): def write_inherit(self, node): self.printer.writeline("def _mako_inherit(context):") self.printer.writeline("_mako_generate_namespaces(context)") - self.printer.writeline("return runtime.inherit_from(context, %s, _template_filename)" % (repr(node.attributes['file']))) + self.printer.writeline("return runtime.inherit_from(context, %s, _template_filename)" % (node.parsed_attributes['file'])) self.printer.writeline(None) def write_namespaces(self, namespaces): diff --git a/lib/mako/parsetree.py b/lib/mako/parsetree.py index d1a35a1..476dbb2 100644 --- a/lib/mako/parsetree.py +++ b/lib/mako/parsetree.py @@ -199,7 +199,8 @@ class Tag(Node): undeclared_identifiers = undeclared_identifiers.union(code.undeclared_identifiers) expr.append(m.group(1)) else: - expr.append(repr(x)) + if x: + expr.append(repr(x)) self.parsed_attributes[key] = " + ".join(expr) elif key in nonexpressions: if re.search(r'${.+?}', self.attributes[key]): diff --git a/lib/mako/runtime.py b/lib/mako/runtime.py index 5444549..c98b914 100644 --- a/lib/mako/runtime.py +++ b/lib/mako/runtime.py @@ -163,6 +163,8 @@ def include_file(context, uri, calling_filename): def inherit_from(context, uri, calling_filename): """called by the _inherit method in template modules to set up the inheritance chain at the start of a template's execution.""" + if uri is None: + return None template = _lookup_template(context, uri, calling_filename) self_ns = context['self'] ih = self_ns @@ -193,7 +195,7 @@ def _populate_self_namespace(context, template, self_ns=None): self_ns = Namespace('self:%s' % template.description, context, template=template, populate_self=False) context._data['self'] = context._data['local'] = self_ns if hasattr(template.module, '_mako_inherit'): - return template.module._mako_inherit(context) + return template.module._mako_inherit(context) or (template.callable_, context) else: return (template.callable_, context) diff --git a/test/inheritance.py b/test/inheritance.py index 28bc78f..fbebcf3 100644 --- a/test/inheritance.py +++ b/test/inheritance.py @@ -180,7 +180,30 @@ ${next.body()} 'a is: layout_a', 'c is: secondary_c. a is layout_a b is base_b d is secondary_d.' ] - - + + def test_dynamic(self): + collection = lookup.TemplateLookup() + collection.put_string("base", """ + this is the base. + ${next.body()} + """) + collection.put_string("index", """ + <%! + def dyn(context): + if context.get('base', None) is not None: + return 'base' + else: + return None + %> + <%inherit file="${dyn(context)}"/> + this is index. + """) + assert result_lines(collection.get_template('index').render()) == [ + 'this is index.' + ] + assert result_lines(collection.get_template('index').render(base=True)) == [ + 'this is the base.', + 'this is index.' + ] if __name__ == '__main__': unittest.main() -- GitLab